Move IFontHandle.ImFontLocked to ILockedImFont+impl

This commit is contained in:
Soreepeong 2024-01-24 00:19:27 +09:00
parent 68dc16803c
commit 5161053cb3
9 changed files with 105 additions and 110 deletions

View file

@ -534,7 +534,7 @@ internal sealed partial class FontAtlasFactory
private void PromoteBuiltData(int rebuildIndex, FontAtlasBuiltData data, [UsedImplicitly] string source)
{
// Capture the locks inside the lock block, so that the fonts are guaranteed to be the ones just built.
var fontsAndLocks = new List<(FontHandle FontHandle, IFontHandle.ImFontLocked Lock)>();
var fontsAndLocks = new List<(FontHandle FontHandle, ILockedImFont Lock)>();
using var garbage = new DisposeSafety.ScopedFinalizer();
lock (this.syncRoot)
@ -557,7 +557,7 @@ internal sealed partial class FontAtlasFactory
foreach (var fontHandle in substance.RelevantHandles)
{
substance.DataRoot.AddRef();
var locked = IFontHandle.ImFontLocked.Rent(
var locked = new LockedImFont(
substance.GetFontPtr(fontHandle),
substance.DataRoot);
fontsAndLocks.Add((fontHandle, garbage.Add(locked)));

View file

@ -74,7 +74,7 @@ internal abstract class FontHandle : IFontHandle
/// Invokes <see cref="IFontHandle.ImFontChanged"/>.
/// </summary>
/// <param name="font">The font, locked during the call of <see cref="ImFontChanged"/>.</param>
public void InvokeImFontChanged(IFontHandle.ImFontLocked font)
public void InvokeImFontChanged(ILockedImFont font)
{
try
{
@ -133,11 +133,11 @@ internal abstract class FontHandle : IFontHandle
/// </summary>
/// <param name="errorMessage">The error message, if any.</param>
/// <returns>
/// An instance of <see cref="IFontHandle.ImFontLocked"/> that <b>must</b> be disposed after use on success;
/// An instance of <see cref="ILockedImFont"/> that <b>must</b> be disposed after use on success;
/// <c>null</c> with <paramref name="errorMessage"/> populated on failure.
/// </returns>
/// <exception cref="ObjectDisposedException">Still may be thrown.</exception>
public IFontHandle.ImFontLocked? TryLock(out string? errorMessage)
public ILockedImFont? TryLock(out string? errorMessage)
{
IFontHandleSubstance? prevSubstance = default;
while (true)
@ -182,12 +182,12 @@ internal abstract class FontHandle : IFontHandle
// Transfer the ownership of reference.
errorMessage = null;
return IFontHandle.ImFontLocked.Rent(fontPtr, substance.DataRoot);
return new LockedImFont(fontPtr, substance.DataRoot);
}
}
/// <inheritdoc/>
public IFontHandle.ImFontLocked Lock() =>
public ILockedImFont Lock() =>
this.TryLock(out var errorMessage) ?? throw new InvalidOperationException(errorMessage);
/// <inheritdoc/>
@ -238,10 +238,10 @@ internal abstract class FontHandle : IFontHandle
this.ImFontChanged += OnImFontChanged;
this.Disposed += OnDisposed;
if (this.Available)
OnImFontChanged(this, default);
OnImFontChanged(this, null);
return tcs.Task;
void OnImFontChanged(IFontHandle unused, IFontHandle.ImFontLocked unused2)
void OnImFontChanged(IFontHandle unused, ILockedImFont? unused2)
{
if (tcs.Task.IsCompletedSuccessfully)
return;

View file

@ -0,0 +1,62 @@
using Dalamud.Utility;
using ImGuiNET;
namespace Dalamud.Interface.ManagedFontAtlas.Internals;
/// <summary>
/// The implementation for <see cref="ILockedImFont"/>.
/// </summary>
internal class LockedImFont : ILockedImFont
{
private IRefCountable? owner;
/// <summary>
/// Initializes a new instance of the <see cref="LockedImFont"/> class.
/// Ownership of reference of <paramref name="owner"/> is transferred.
/// </summary>
/// <param name="font">The contained font.</param>
/// <param name="owner">The owner.</param>
/// <returns>The rented instance of <see cref="LockedImFont"/>.</returns>
internal LockedImFont(ImFontPtr font, IRefCountable owner)
{
this.ImFont = font;
this.owner = owner;
}
/// <summary>
/// Finalizes an instance of the <see cref="LockedImFont"/> class.
/// </summary>
~LockedImFont() => this.FreeOwner();
/// <inheritdoc/>
public ImFontPtr ImFont { get; private set; }
/// <inheritdoc/>
public ILockedImFont NewRef()
{
if (this.owner is null)
throw new ObjectDisposedException(nameof(LockedImFont));
var newRef = new LockedImFont(this.ImFont, this.owner);
this.owner.AddRef();
return newRef;
}
/// <inheritdoc/>
public void Dispose()
{
this.FreeOwner();
GC.SuppressFinalize(this);
}
private void FreeOwner()
{
if (this.owner is null)
return;
this.owner.Release();
this.owner = null;
this.ImFont = default;
}
}