Add texture leak tracker

This commit is contained in:
Soreepeong 2024-03-05 01:06:02 +09:00
parent 2572f24e08
commit 6a0f774625
14 changed files with 597 additions and 63 deletions

View file

@ -443,6 +443,9 @@ internal sealed class DalamudConfiguration : IServiceType, IDisposable
/// </summary> /// </summary>
public double UiBuilderHitch { get; set; } = 100; public double UiBuilderHitch { get; set; } = 100;
/// <summary>Gets or sets a value indicating whether to track texture allocation by plugins.</summary>
public bool UseTexturePluginTracking { get; set; }
/// <summary> /// <summary>
/// Gets or sets the page of the plugin installer that is shown by default when opened. /// Gets or sets the page of the plugin installer that is shown by default when opened.
/// </summary> /// </summary>

View file

@ -6,6 +6,7 @@ using System.Reflection;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dalamud.Configuration.Internal;
using Dalamud.Interface.Components; using Dalamud.Interface.Components;
using Dalamud.Interface.ImGuiFileDialog; using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Internal.Notifications;
@ -108,6 +109,11 @@ internal class TexWidget : IDataWindowWidget
if (ImGui.Button("GC")) if (ImGui.Button("GC"))
GC.Collect(); GC.Collect();
ImGui.PushID("blames");
if (ImGui.CollapsingHeader($"All Loaded Textures: {this.textureManager.AllBlamesForDebug.Count:g}###header"))
this.DrawBlame(this.textureManager.AllBlamesForDebug);
ImGui.PopID();
ImGui.PushID("loadedGameTextures"); ImGui.PushID("loadedGameTextures");
if (ImGui.CollapsingHeader( if (ImGui.CollapsingHeader(
$"Loaded Game Textures: {this.textureManager.Shared.ForDebugGamePathTextures.Count:g}###header")) $"Loaded Game Textures: {this.textureManager.Shared.ForDebugGamePathTextures.Count:g}###header"))
@ -292,6 +298,96 @@ internal class TexWidget : IDataWindowWidget
this.fileDialogManager.Draw(); this.fileDialogManager.Draw();
} }
private unsafe void DrawBlame(IReadOnlyList<TextureManager.IBlameableDalamudTextureWrap> allBlames)
{
var conf = Service<DalamudConfiguration>.Get();
var im = Service<InterfaceManager>.Get();
var blame = conf.UseTexturePluginTracking;
if (ImGui.Checkbox("Enable", ref blame))
{
conf.UseTexturePluginTracking = blame;
conf.QueueSave();
}
if (!ImGui.BeginTable("##table", 5))
return;
const int numIcons = 1;
float iconWidths;
using (im.IconFontHandle?.Push())
iconWidths = ImGui.CalcTextSize(FontAwesomeIcon.Save.ToIconString()).X;
ImGui.TableSetupScrollFreeze(0, 1);
ImGui.TableSetupColumn("Size", ImGuiTableColumnFlags.WidthFixed, ImGui.CalcTextSize("00000x00000").X);
ImGui.TableSetupColumn(
"Format",
ImGuiTableColumnFlags.WidthFixed,
ImGui.CalcTextSize("R32G32B32A32_TYPELESS").X);
ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthStretch);
ImGui.TableSetupColumn(
"Actions",
ImGuiTableColumnFlags.WidthFixed,
iconWidths +
(ImGui.GetStyle().FramePadding.X * 2 * numIcons) +
(ImGui.GetStyle().ItemSpacing.X * 1 * numIcons));
ImGui.TableSetupColumn(
"Plugins",
ImGuiTableColumnFlags.WidthFixed,
ImGui.CalcTextSize("Aaaaaaaaaa Aaaaaaaaaa Aaaaaaaaaa").X);
ImGui.TableHeadersRow();
var clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper());
clipper.Begin(allBlames.Count);
while (clipper.Step())
{
for (var i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
var wrap = allBlames[i];
ImGui.TableNextRow();
ImGui.PushID(i);
ImGui.TableNextColumn();
ImGui.TextUnformatted($"{wrap.Width}x{wrap.Height}");
ImGui.TableNextColumn();
ImGui.TextUnformatted(Enum.GetName(wrap.Format)?[12..] ?? wrap.Format.ToString());
ImGui.TableNextColumn();
ImGui.TextUnformatted(wrap.Name);
ImGui.TableNextColumn();
if (ImGuiComponents.IconButton(FontAwesomeIcon.Save))
{
this.SaveTextureAsync(
$"{wrap.ImGuiHandle:X16}",
() => Task.FromResult(wrap.CreateWrapSharingLowLevelResource()));
}
if (ImGui.IsItemHovered())
{
ImGui.BeginTooltip();
ImGui.Image(wrap.ImGuiHandle, wrap.Size);
ImGui.EndTooltip();
}
ImGui.TableNextColumn();
lock (wrap.OwnerPlugins)
{
foreach (var plugin in wrap.OwnerPlugins)
ImGui.TextUnformatted(plugin.Name);
}
ImGui.PopID();
}
}
clipper.Destroy();
ImGui.EndTable();
ImGuiHelpers.ScaledDummy(10);
}
private unsafe void DrawLoadedTextures(ICollection<SharedImmediateTexture> textures) private unsafe void DrawLoadedTextures(ICollection<SharedImmediateTexture> textures)
{ {
var im = Service<InterfaceManager>.Get(); var im = Service<InterfaceManager>.Get();
@ -354,6 +450,7 @@ internal class TexWidget : IDataWindowWidget
} }
var remain = texture.SelfReferenceExpiresInForDebug; var remain = texture.SelfReferenceExpiresInForDebug;
ImGui.PushID(row);
ImGui.TableNextColumn(); ImGui.TableNextColumn();
ImGui.AlignTextToFramePadding(); ImGui.AlignTextToFramePadding();
@ -400,6 +497,8 @@ internal class TexWidget : IDataWindowWidget
ImGui.SetTooltip("Release self-reference immediately."); ImGui.SetTooltip("Release self-reference immediately.");
if (remain <= 0) if (remain <= 0)
ImGui.EndDisabled(); ImGui.EndDisabled();
ImGui.PopID();
} }
if (!valid) if (!valid)
@ -609,7 +708,8 @@ internal class TexWidget : IDataWindowWidget
new() new()
{ {
Api10 = this.textureManager.CreateFromImGuiViewportAsync( Api10 = this.textureManager.CreateFromImGuiViewportAsync(
this.viewportTextureArgs with { ViewportId = viewports[this.viewportIndexInt].ID }), this.viewportTextureArgs with { ViewportId = viewports[this.viewportIndexInt].ID },
null),
}); });
} }
} }
@ -758,6 +858,9 @@ internal class TexWidget : IDataWindowWidget
} }
catch (Exception e) catch (Exception e)
{ {
if (e is OperationCanceledException)
return;
Log.Error(e, $"{nameof(TexWidget)}.{nameof(this.SaveTextureAsync)}"); Log.Error(e, $"{nameof(TexWidget)}.{nameof(this.SaveTextureAsync)}");
Service<NotificationManager>.Get().AddNotification( Service<NotificationManager>.Get().AddNotification(
$"Failed to save file: {e}", $"Failed to save file: {e}",

View file

@ -45,13 +45,11 @@ internal sealed partial class FontAtlasFactory
DataManager dataManager, DataManager dataManager,
Framework framework, Framework framework,
InterfaceManager interfaceManager, InterfaceManager interfaceManager,
DalamudAssetManager dalamudAssetManager, DalamudAssetManager dalamudAssetManager)
TextureManager textureManager)
{ {
this.Framework = framework; this.Framework = framework;
this.InterfaceManager = interfaceManager; this.InterfaceManager = interfaceManager;
this.dalamudAssetManager = dalamudAssetManager; this.dalamudAssetManager = dalamudAssetManager;
this.TextureManager = textureManager;
this.SceneTask = Service<InterfaceManager.InterfaceManagerWithScene> this.SceneTask = Service<InterfaceManager.InterfaceManagerWithScene>
.GetAsync() .GetAsync()
.ContinueWith(r => r.Result.Manager.Scene); .ContinueWith(r => r.Result.Manager.Scene);
@ -148,7 +146,7 @@ internal sealed partial class FontAtlasFactory
/// <summary> /// <summary>
/// Gets the service instance of <see cref="TextureManager"/>. /// Gets the service instance of <see cref="TextureManager"/>.
/// </summary> /// </summary>
public TextureManager TextureManager { get; } public TextureManager TextureManager => Service<TextureManager>.Get();
/// <summary> /// <summary>
/// Gets the async task for <see cref="RawDX11Scene"/> inside <see cref="InterfaceManager"/>. /// Gets the async task for <see cref="RawDX11Scene"/> inside <see cref="InterfaceManager"/>.

View file

@ -28,6 +28,8 @@ internal sealed class FileSystemSharedImmediateTexture : SharedImmediateTexture
protected override async Task<IDalamudTextureWrap> CreateTextureAsync(CancellationToken cancellationToken) protected override async Task<IDalamudTextureWrap> CreateTextureAsync(CancellationToken cancellationToken)
{ {
var tm = await Service<TextureManager>.GetAsync(); 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;
} }
} }

View file

@ -37,6 +37,8 @@ internal sealed class GamePathSharedImmediateTexture : SharedImmediateTexture
if (dm.GetFile<TexFile>(substPath) is not { } file) if (dm.GetFile<TexFile>(substPath) is not { } file)
throw new FileNotFoundException(); throw new FileNotFoundException();
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
return tm.NoThrottleCreateFromTexFile(file); var wrap = tm.NoThrottleCreateFromTexFile(file);
tm.BlameSetName(wrap, this.ToString());
return wrap;
} }
} }

View file

@ -40,6 +40,8 @@ internal sealed class ManifestResourceSharedImmediateTexture : SharedImmediateTe
var tm = await Service<TextureManager>.GetAsync(); var tm = await Service<TextureManager>.GetAsync();
var ms = new MemoryStream(stream.CanSeek ? checked((int)stream.Length) : 0); var ms = new MemoryStream(stream.CanSeek ? checked((int)stream.Length) : 0);
await stream.CopyToAsync(ms, cancellationToken); 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;
} }
} }

View file

@ -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;
}
}

View file

@ -3,6 +3,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Services; using Dalamud.Plugin.Services;
using Dalamud.Utility; using Dalamud.Utility;
@ -71,23 +72,34 @@ internal sealed partial class TextureManager
var desc = default(D3D11_TEXTURE2D_DESC); var desc = default(D3D11_TEXTURE2D_DESC);
tex.Get()->GetDesc(&desc); tex.Get()->GetDesc(&desc);
return new UnknownTextureWrap(
var outWrap = new UnknownTextureWrap(
(IUnknown*)srv.Get(), (IUnknown*)srv.Get(),
(int)desc.Width, (int)desc.Width,
(int)desc.Height, (int)desc.Height,
true); true);
this.BlameSetName(
outWrap,
$"{nameof(this.CreateFromExistingTextureAsync)}({nameof(wrap)}, {nameof(args)}, {nameof(leaveWrapOpen)}, {nameof(cancellationToken)})");
return outWrap;
} }
}, },
cancellationToken, cancellationToken,
leaveWrapOpen ? null : wrap); leaveWrapOpen ? null : wrap);
/// <inheritdoc/> /// <inheritdoc/>
Task<IDalamudTextureWrap> ITextureProvider.CreateFromImGuiViewportAsync(
ImGuiViewportTextureArgs args,
CancellationToken cancellationToken) => this.CreateFromImGuiViewportAsync(args, null, cancellationToken);
/// <inheritdoc cref="ITextureProvider.CreateFromImGuiViewportAsync"/>
public Task<IDalamudTextureWrap> CreateFromImGuiViewportAsync( public Task<IDalamudTextureWrap> CreateFromImGuiViewportAsync(
ImGuiViewportTextureArgs args, ImGuiViewportTextureArgs args,
LocalPlugin? ownerPlugin,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
args.ThrowOnInvalidValues(); args.ThrowOnInvalidValues();
var t = new ViewportTextureWrap(args, cancellationToken); var t = new ViewportTextureWrap(args, ownerPlugin, cancellationToken);
t.QueueUpdate(); t.QueueUpdate();
return t.FirstUpdateTask; return t.FirstUpdateTask;
} }

View file

@ -1,5 +1,3 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
@ -11,7 +9,6 @@ using Dalamud.Game;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
using Dalamud.Interface.Textures.Internal.SharedImmediateTextures; using Dalamud.Interface.Textures.Internal.SharedImmediateTextures;
using Dalamud.Logging.Internal; using Dalamud.Logging.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Services; using Dalamud.Plugin.Services;
using Dalamud.Utility; using Dalamud.Utility;
@ -64,6 +61,8 @@ internal sealed partial class TextureManager
failsafe.Add(this.sharedTextureManager = new(this)); failsafe.Add(this.sharedTextureManager = new(this));
failsafe.Add(this.wicManager = new(this)); failsafe.Add(this.wicManager = new(this));
failsafe.Add(this.simpleDrawer = new()); failsafe.Add(this.simpleDrawer = new());
this.framework.Update += this.BlameTrackerUpdate;
failsafe.Add(() => this.framework.Update -= this.BlameTrackerUpdate);
this.simpleDrawer.Setup(this.device.Get()); this.simpleDrawer.Setup(this.device.Get());
failsafe.Cancel(); failsafe.Cancel();
@ -116,21 +115,18 @@ internal sealed partial class TextureManager
GC.SuppressFinalize(this); 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/> /// <inheritdoc/>
public Task<IDalamudTextureWrap> CreateFromImageAsync( public Task<IDalamudTextureWrap> CreateFromImageAsync(
ReadOnlyMemory<byte> bytes, ReadOnlyMemory<byte> bytes,
CancellationToken cancellationToken = default) => CancellationToken cancellationToken = default) =>
this.DynamicPriorityTextureLoader.LoadAsync( this.DynamicPriorityTextureLoader.LoadAsync(
null, 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); cancellationToken);
/// <inheritdoc/> /// <inheritdoc/>
@ -144,7 +140,9 @@ internal sealed partial class TextureManager
{ {
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
await stream.CopyToAsync(ms, ct).ConfigureAwait(false); 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, cancellationToken,
leaveOpen ? null : stream); 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. // It probably doesn't make sense to throttle this, as it copies the passed bytes to GPU without any transformation.
public IDalamudTextureWrap CreateFromRaw( public IDalamudTextureWrap CreateFromRaw(
RawImageSpecification specs, 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/> /// <inheritdoc/>
public Task<IDalamudTextureWrap> CreateFromRawAsync( public Task<IDalamudTextureWrap> CreateFromRawAsync(
@ -162,7 +163,10 @@ internal sealed partial class TextureManager
CancellationToken cancellationToken = default) => CancellationToken cancellationToken = default) =>
this.DynamicPriorityTextureLoader.LoadAsync( this.DynamicPriorityTextureLoader.LoadAsync(
null, 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); cancellationToken);
/// <inheritdoc/> /// <inheritdoc/>
@ -177,13 +181,18 @@ internal sealed partial class TextureManager
{ {
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
await stream.CopyToAsync(ms, ct).ConfigureAwait(false); 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, cancellationToken,
leaveOpen ? null : stream); leaveOpen ? null : stream);
/// <inheritdoc/> /// <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/> /// <inheritdoc/>
public Task<IDalamudTextureWrap> CreateFromTexFileAsync( public Task<IDalamudTextureWrap> CreateFromTexFileAsync(
@ -191,7 +200,10 @@ internal sealed partial class TextureManager
CancellationToken cancellationToken = default) => CancellationToken cancellationToken = default) =>
this.DynamicPriorityTextureLoader.LoadAsync( this.DynamicPriorityTextureLoader.LoadAsync(
null, null,
_ => Task.FromResult(this.NoThrottleCreateFromTexFile(file)), _ => Task.FromResult(
this.BlameSetName(
this.NoThrottleCreateFromTexFile(file),
$"{nameof(this.CreateFromTexFile)}({nameof(file)})")),
cancellationToken); cancellationToken);
/// <inheritdoc/> /// <inheritdoc/>
@ -244,7 +256,9 @@ internal sealed partial class TextureManager
this.device.Get()->CreateShaderResourceView((ID3D11Resource*)texture.Get(), &viewDesc, view.GetAddressOf()) this.device.Get()->CreateShaderResourceView((ID3D11Resource*)texture.Get(), &viewDesc, view.GetAddressOf())
.ThrowOnError(); .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 /// <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); buffer = buffer.Filter(0, 0, TexFile.TextureFormat.B8G8R8A8);
} }
return this.NoThrottleCreateFromRaw( var wrap = this.NoThrottleCreateFromRaw(new(buffer.Width, buffer.Height, dxgiFormat), buffer.RawData);
new(buffer.Width, buffer.Height, dxgiFormat), this.BlameSetName(wrap, $"{nameof(this.NoThrottleCreateFromTexFile)}({nameof(file)})");
buffer.RawData); return wrap;
} }
/// <summary>Creates a texture from the given <paramref name="fileBytes"/>, trying to interpret it as a /// <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, tf,
new object?[] { new LuminaBinaryReader(bytesArray) }); new object?[] { new LuminaBinaryReader(bytesArray) });
// Note: FileInfo and FilePath are not used from TexFile; skip it. // 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(); private void ReleaseUnmanagedResources() => this.device.Reset();

View file

@ -40,8 +40,10 @@ internal sealed partial class TextureManagerPluginScoped
private Task<TextureManager>? managerTaskNullable; private Task<TextureManager>? managerTaskNullable;
/// <summary>Initializes a new instance of the <see cref="TextureManagerPluginScoped"/> class.</summary>
/// <param name="plugin">The plugin.</param>
[ServiceManager.ServiceConstructor] [ServiceManager.ServiceConstructor]
private TextureManagerPluginScoped(LocalPlugin plugin) public TextureManagerPluginScoped(LocalPlugin plugin)
{ {
this.plugin = plugin; this.plugin = plugin;
if (plugin.Manifest is LocalPluginManifest lpm) if (plugin.Manifest is LocalPluginManifest lpm)
@ -155,7 +157,7 @@ internal sealed partial class TextureManagerPluginScoped
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var manager = await this.ManagerTask; 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); manager.Blame(textureWrap, this.plugin);
return textureWrap; return textureWrap;
} }

View file

@ -4,6 +4,8 @@ using System.Threading.Tasks;
using Dalamud.Game; using Dalamud.Game;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Storage.Assets;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET; 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> /// <summary>A texture wrap that takes its buffer from the frame buffer (of swap chain).</summary>
internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDisposable internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDisposable
{ {
private readonly LocalPlugin? ownerPlugin;
private readonly CancellationToken cancellationToken; private readonly CancellationToken cancellationToken;
private readonly TaskCompletionSource<IDalamudTextureWrap> firstUpdateTaskCompletionSource = new(); 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> /// <summary>Initializes a new instance of the <see cref="ViewportTextureWrap"/> class.</summary>
/// <param name="args">The arguments for creating a texture.</param> /// <param name="args">The arguments for creating a texture.</param>
/// <param name="ownerPlugin">The owner plugin.</param>
/// <param name="cancellationToken">The cancellation token.</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.args = args;
this.ownerPlugin = ownerPlugin;
this.cancellationToken = cancellationToken; this.cancellationToken = cancellationToken;
} }
@ -42,7 +48,14 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
~ViewportTextureWrap() => this.Dispose(false); ~ViewportTextureWrap() => this.Dispose(false);
/// <inheritdoc/> /// <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/> /// <inheritdoc/>
public int Width => (int)this.desc.Width; public int Width => (int)this.desc.Width;
@ -134,6 +147,11 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
srvTemp.Swap(ref this.srv); srvTemp.Swap(ref this.srv);
rtvTemp.Swap(ref this.rtv); rtvTemp.Swap(ref this.rtv);
texTemp.Swap(ref this.tex); 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()); // context.Get()->CopyResource((ID3D11Resource*)this.tex.Get(), (ID3D11Resource*)backBuffer.Get());

View file

@ -42,6 +42,7 @@ public sealed class UiBuilder : IDisposable
private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get(); private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get();
private readonly DisposeSafety.ScopedFinalizer scopedFinalizer = new(); private readonly DisposeSafety.ScopedFinalizer scopedFinalizer = new();
private readonly TextureManagerPluginScoped scopedTextureProvider;
private bool hasErrorWindow = false; private bool hasErrorWindow = false;
private bool lastFrameUiHideState = false; private bool lastFrameUiHideState = false;
@ -54,8 +55,9 @@ public sealed class UiBuilder : IDisposable
/// Initializes a new instance of the <see cref="UiBuilder"/> class and registers it. /// Initializes a new instance of the <see cref="UiBuilder"/> class and registers it.
/// You do not have to call this manually. /// You do not have to call this manually.
/// </summary> /// </summary>
/// <param name="plugin">The plugin.</param>
/// <param name="namespaceName">The plugin namespace.</param> /// <param name="namespaceName">The plugin namespace.</param>
internal UiBuilder(string namespaceName) internal UiBuilder(LocalPlugin plugin, string namespaceName)
{ {
try try
{ {
@ -69,6 +71,8 @@ public sealed class UiBuilder : IDisposable
this.interfaceManager.ResizeBuffers += this.OnResizeBuffers; this.interfaceManager.ResizeBuffers += this.OnResizeBuffers;
this.scopedFinalizer.Add(() => this.interfaceManager.ResizeBuffers -= this.OnResizeBuffers); this.scopedFinalizer.Add(() => this.interfaceManager.ResizeBuffers -= this.OnResizeBuffers);
this.scopedFinalizer.Add(this.scopedTextureProvider = new(plugin));
this.FontAtlas = this.FontAtlas =
this.scopedFinalizer this.scopedFinalizer
.Add( .Add(
@ -381,8 +385,6 @@ public sealed class UiBuilder : IDisposable
private Task<InterfaceManager> InterfaceManagerWithSceneAsync => private Task<InterfaceManager> InterfaceManagerWithSceneAsync =>
Service<InterfaceManager.InterfaceManagerWithScene>.GetAsync().ContinueWith(task => task.Result.Manager); Service<InterfaceManager.InterfaceManagerWithScene>.GetAsync().ContinueWith(task => task.Result.Manager);
private ITextureProvider TextureProvider => Service<TextureManager>.Get();
/// <summary> /// <summary>
/// Loads an image from the specified file. /// Loads an image from the specified file.
/// </summary> /// </summary>
@ -391,7 +393,7 @@ public sealed class UiBuilder : IDisposable
[Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)] [Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)]
[Obsolete($"Use {nameof(ITextureProvider.GetFromFile)}.")] [Obsolete($"Use {nameof(ITextureProvider.GetFromFile)}.")]
public IDalamudTextureWrap LoadImage(string filePath) => public IDalamudTextureWrap LoadImage(string filePath) =>
this.TextureProvider.GetFromFile(filePath).RentAsync().Result; this.scopedTextureProvider.GetFromFile(filePath).RentAsync().Result;
/// <summary> /// <summary>
/// Loads an image from a byte stream, such as a png downloaded into memory. /// Loads an image from a byte stream, such as a png downloaded into memory.
@ -401,7 +403,7 @@ public sealed class UiBuilder : IDisposable
[Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)] [Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)]
[Obsolete($"Use {nameof(ITextureProvider.CreateFromImageAsync)}.")] [Obsolete($"Use {nameof(ITextureProvider.CreateFromImageAsync)}.")]
public IDalamudTextureWrap LoadImage(byte[] imageData) => public IDalamudTextureWrap LoadImage(byte[] imageData) =>
this.TextureProvider.CreateFromImageAsync(imageData).Result; this.scopedTextureProvider.CreateFromImageAsync(imageData).Result;
/// <summary> /// <summary>
/// Loads an image from raw unformatted pixel data, with no type or header information. To load formatted data, use <see cref="LoadImage(byte[])"/>. /// Loads an image from raw unformatted pixel data, with no type or header information. To load formatted data, use <see cref="LoadImage(byte[])"/>.
@ -416,7 +418,7 @@ public sealed class UiBuilder : IDisposable
public IDalamudTextureWrap LoadImageRaw(byte[] imageData, int width, int height, int numChannels) => public IDalamudTextureWrap LoadImageRaw(byte[] imageData, int width, int height, int numChannels) =>
numChannels switch numChannels switch
{ {
4 => this.TextureProvider.CreateFromRaw(RawImageSpecification.Rgba32(width, height), imageData), 4 => this.scopedTextureProvider.CreateFromRaw(RawImageSpecification.Rgba32(width, height), imageData),
_ => throw new NotSupportedException(), _ => throw new NotSupportedException(),
}; };
@ -436,7 +438,7 @@ public sealed class UiBuilder : IDisposable
[Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)] [Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)]
[Obsolete($"Use {nameof(ITextureProvider.GetFromFile)}.")] [Obsolete($"Use {nameof(ITextureProvider.GetFromFile)}.")]
public Task<IDalamudTextureWrap> LoadImageAsync(string filePath) => public Task<IDalamudTextureWrap> LoadImageAsync(string filePath) =>
this.TextureProvider.GetFromFile(filePath).RentAsync(); this.scopedTextureProvider.GetFromFile(filePath).RentAsync();
/// <summary> /// <summary>
/// Asynchronously loads an image from a byte stream, such as a png downloaded into memory, when it's possible to do so. /// Asynchronously loads an image from a byte stream, such as a png downloaded into memory, when it's possible to do so.
@ -446,7 +448,7 @@ public sealed class UiBuilder : IDisposable
[Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)] [Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)]
[Obsolete($"Use {nameof(ITextureProvider.CreateFromImageAsync)}.")] [Obsolete($"Use {nameof(ITextureProvider.CreateFromImageAsync)}.")]
public Task<IDalamudTextureWrap> LoadImageAsync(byte[] imageData) => public Task<IDalamudTextureWrap> LoadImageAsync(byte[] imageData) =>
this.TextureProvider.CreateFromImageAsync(imageData); this.scopedTextureProvider.CreateFromImageAsync(imageData);
/// <summary> /// <summary>
/// Asynchronously loads an image from raw unformatted pixel data, with no type or header information, when it's possible to do so. To load formatted data, use <see cref="LoadImage(byte[])"/>. /// Asynchronously loads an image from raw unformatted pixel data, with no type or header information, when it's possible to do so. To load formatted data, use <see cref="LoadImage(byte[])"/>.
@ -461,7 +463,7 @@ public sealed class UiBuilder : IDisposable
public Task<IDalamudTextureWrap> LoadImageRawAsync(byte[] imageData, int width, int height, int numChannels) => public Task<IDalamudTextureWrap> LoadImageRawAsync(byte[] imageData, int width, int height, int numChannels) =>
numChannels switch numChannels switch
{ {
4 => this.TextureProvider.CreateFromRawAsync(RawImageSpecification.Rgba32(width, height), imageData), 4 => this.scopedTextureProvider.CreateFromRawAsync(RawImageSpecification.Rgba32(width, height), imageData),
_ => Task.FromException<IDalamudTextureWrap>(new NotSupportedException()), _ => Task.FromException<IDalamudTextureWrap>(new NotSupportedException()),
}; };

View file

@ -52,7 +52,7 @@ public sealed class DalamudPluginInterface : IDisposable
var dataManager = Service<DataManager>.Get(); var dataManager = Service<DataManager>.Get();
var localization = Service<Localization>.Get(); var localization = Service<Localization>.Get();
this.UiBuilder = new UiBuilder(plugin.Name); this.UiBuilder = new(plugin, plugin.Name);
this.configs = Service<PluginManager>.Get().PluginConfigs; this.configs = Service<PluginManager>.Get().PluginConfigs;
this.Reason = reason; this.Reason = reason;

View file

@ -54,64 +54,64 @@ internal sealed unsafe class ManagedIStream : IStream.Interface, IRefCountable
return; return;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static IStream.Interface? ToManagedObject(void* pThis) => static ManagedIStream? ToManagedObject(void* pThis) =>
GCHandle.FromIntPtr(((nint*)pThis)[1]).Target as IStream.Interface; GCHandle.FromIntPtr(((nint*)pThis)[1]).Target as ManagedIStream;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int QueryInterfaceStatic(IStream* pThis, Guid* riid, void** ppvObject) => static int QueryInterfaceStatic(IStream* pThis, Guid* riid, void** ppvObject) =>
ToManagedObject(pThis)?.QueryInterface(riid, ppvObject) ?? E.E_FAIL; ToManagedObject(pThis)?.QueryInterface(riid, ppvObject) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static uint AddRefStatic(IStream* pThis) => ToManagedObject(pThis)?.AddRef() ?? 0; static uint AddRefStatic(IStream* pThis) => (uint)(ToManagedObject(pThis)?.AddRef() ?? 0);
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static uint ReleaseStatic(IStream* pThis) => ToManagedObject(pThis)?.Release() ?? 0; static uint ReleaseStatic(IStream* pThis) => (uint)(ToManagedObject(pThis)?.Release() ?? 0);
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int ReadStatic(IStream* pThis, void* pv, uint cb, uint* pcbRead) => static int ReadStatic(IStream* pThis, void* pv, uint cb, uint* pcbRead) =>
ToManagedObject(pThis)?.Read(pv, cb, pcbRead) ?? E.E_FAIL; ToManagedObject(pThis)?.Read(pv, cb, pcbRead) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int WriteStatic(IStream* pThis, void* pv, uint cb, uint* pcbWritten) => static int WriteStatic(IStream* pThis, void* pv, uint cb, uint* pcbWritten) =>
ToManagedObject(pThis)?.Write(pv, cb, pcbWritten) ?? E.E_FAIL; ToManagedObject(pThis)?.Write(pv, cb, pcbWritten) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int SeekStatic( static int SeekStatic(
IStream* pThis, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition) => IStream* pThis, LARGE_INTEGER dlibMove, uint dwOrigin, ULARGE_INTEGER* plibNewPosition) =>
ToManagedObject(pThis)?.Seek(dlibMove, dwOrigin, plibNewPosition) ?? E.E_FAIL; ToManagedObject(pThis)?.Seek(dlibMove, dwOrigin, plibNewPosition) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int SetSizeStatic(IStream* pThis, ULARGE_INTEGER libNewSize) => static int SetSizeStatic(IStream* pThis, ULARGE_INTEGER libNewSize) =>
ToManagedObject(pThis)?.SetSize(libNewSize) ?? E.E_FAIL; ToManagedObject(pThis)?.SetSize(libNewSize) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int CopyToStatic( static int CopyToStatic(
IStream* pThis, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, IStream* pThis, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead,
ULARGE_INTEGER* pcbWritten) => ULARGE_INTEGER* pcbWritten) =>
ToManagedObject(pThis)?.CopyTo(pstm, cb, pcbRead, pcbWritten) ?? E.E_FAIL; ToManagedObject(pThis)?.CopyTo(pstm, cb, pcbRead, pcbWritten) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int CommitStatic(IStream* pThis, uint grfCommitFlags) => static int CommitStatic(IStream* pThis, uint grfCommitFlags) =>
ToManagedObject(pThis)?.Commit(grfCommitFlags) ?? E.E_FAIL; ToManagedObject(pThis)?.Commit(grfCommitFlags) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int RevertStatic(IStream* pThis) => ToManagedObject(pThis)?.Revert() ?? E.E_FAIL; static int RevertStatic(IStream* pThis) => ToManagedObject(pThis)?.Revert() ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int LockRegionStatic(IStream* pThis, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) => static int LockRegionStatic(IStream* pThis, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) =>
ToManagedObject(pThis)?.LockRegion(libOffset, cb, dwLockType) ?? E.E_FAIL; ToManagedObject(pThis)?.LockRegion(libOffset, cb, dwLockType) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int UnlockRegionStatic( static int UnlockRegionStatic(
IStream* pThis, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) => IStream* pThis, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, uint dwLockType) =>
ToManagedObject(pThis)?.UnlockRegion(libOffset, cb, dwLockType) ?? E.E_FAIL; ToManagedObject(pThis)?.UnlockRegion(libOffset, cb, dwLockType) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int StatStatic(IStream* pThis, STATSTG* pstatstg, uint grfStatFlag) => static int StatStatic(IStream* pThis, STATSTG* pstatstg, uint grfStatFlag) =>
ToManagedObject(pThis)?.Stat(pstatstg, grfStatFlag) ?? E.E_FAIL; ToManagedObject(pThis)?.Stat(pstatstg, grfStatFlag) ?? E.E_UNEXPECTED;
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
static int CloneStatic(IStream* pThis, IStream** ppstm) => ToManagedObject(pThis)?.Clone(ppstm) ?? E.E_FAIL; static int CloneStatic(IStream* pThis, IStream** ppstm) => ToManagedObject(pThis)?.Clone(ppstm) ?? E.E_UNEXPECTED;
} }
/// <inheritdoc cref="INativeGuid.NativeGuid"/> /// <inheritdoc cref="INativeGuid.NativeGuid"/>