Move ComPtr<T> static helpers to ComPtr type

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-08 21:07:46 +02:00
1 親 10ed9aad0d
コミット f37e4446f6
7ファイル変更
+50 -39

検索条件に一致するファイルはありません

@@ -311,7 +311,7 @@ partial struct PixelShaderEffect
try
{
// Store the new ID2D1DrawInfo object
ComPtr<ID2D1DrawInfo>.CopyTo(drawInfo, ref @this->d2D1DrawInfo);
ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo);
Guid shaderId = @this->GetGlobals().EffectId;
D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions;
@@ -115,7 +115,7 @@ internal unsafe partial struct PixelShaderEffect
transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert();
// Store the new ID2D1EffectContext object
ComPtr<ID2D1EffectContext>.CopyTo(effectContext, ref @this->d2D1EffectContext);
ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext);
return S.S_OK;
}
+2 -2
ファイルの表示
@@ -251,7 +251,7 @@ unsafe partial struct PixelShaderEffect
unknown.CopyTo(transformMapper.GetAddressOf()).Assert();
// If there's already an existing manager, release it
ComPtr<ID2D1TransformMapper>.Release(@this->d2D1TransformMapper);
ComPtr.Release(@this->d2D1TransformMapper);
// Store the transform mapper manager into the effect
@this->d2D1TransformMapper = transformMapper.Detach();
@@ -343,7 +343,7 @@ unsafe partial struct PixelShaderEffect
ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index];
// If there's already an existing manager at this index, release it
ComPtr<ID2D1ResourceTextureManager>.Release(currentResourceTextureManager);
ComPtr.Release(currentResourceTextureManager);
// Store the resource texture manager into the buffer
currentResourceTextureManager = resourceTextureManager.Detach();
+4 -5
ファイルの表示
@@ -2,7 +2,6 @@ using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -232,16 +231,16 @@ internal unsafe partial struct PixelShaderEffect
NativeMemory.Free(this.constantBuffer);
ComPtr<ID2D1TransformMapper>.Release(this.d2D1TransformMapper);
ComPtr<ID2D1DrawInfo>.Release(this.d2D1DrawInfo);
ComPtr<ID2D1EffectContext>.Release(this.d2D1EffectContext);
ComPtr.Release(this.d2D1TransformMapper);
ComPtr.Release(this.d2D1DrawInfo);
ComPtr.Release(this.d2D1EffectContext);
// Retrieve all possible resource texture managers in use and release the ones that had been
// assigned (from one of the property bindings). We just hardcode 16 here and dont access
// the globals, as technically invoking APIs on it might throw an exception.
for (int i = 0; i < 16; i++)
{
ComPtr<ID2D1ResourceTextureManager>.Release(this.resourceTextureManagerBuffer[i]);
ComPtr.Release(this.resourceTextureManagerBuffer[i]);
}
NativeMemory.Free(Unsafe.AsPointer(ref this));
@@ -124,7 +124,7 @@ partial struct D2D1ResourceTextureManagerImpl
// Now, the effect context can actually be stored safely while holding the lock.
// This is guaranteed to be the case here, as this method is only called (as per
// contact of the COM interface) from ID2D1EffectImpl, which holds the D2D lock.
ComPtr<ID2D1EffectContext>.WriteTo(effectContext, out @this->d2D1EffectContext);
ComPtr.WriteTo(effectContext, out @this->d2D1EffectContext);
@this->d2D1Multithread = d2D1Multithread.Detach();
}
@@ -161,7 +161,7 @@ partial struct D2D1ResourceTextureManagerImpl
// If the texture has already been created, just return it
if (@this->d2D1ResourceTexture is not null)
{
ComPtr<ID2D1ResourceTexture>.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture);
ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture);
return S.S_OK;
}
@@ -249,7 +249,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
this.d2D1Multithread->Enter();
// Release the resource too if it has been created
ComPtr<ID2D1ResourceTexture>.Release(this.d2D1ResourceTexture);
ComPtr.Release(this.d2D1ResourceTexture);
// Now that the resource, if any, has been released, the effect context can also
// be released. This must be done only after any associated resource textures have
+39 -27
ファイルの表示
@@ -13,37 +13,19 @@ using System.Diagnostics.CodeAnalysis;
namespace TerraFX.Interop.Windows;
/// <summary>A type that allows working with pointers to COM objects more securely.</summary>
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
/// <remarks>While this type is not marked as <see langword="ref"/> so that it can also be used in fields, make sure to keep the reference counts properly tracked if you do store <see cref="ComPtr{T}"/> instances on the heap.</remarks>
internal unsafe struct ComPtr<T> : IDisposable
where T : unmanaged
/// <summary>
/// Extensions for <see cref="ComPtr{T}"/> to operate on raw pointers as well.
/// </summary>
internal static unsafe class ComPtr
{
/// <summary>The raw pointer to a COM object, if existing.</summary>
private T* ptr_;
/// <summary>Creates a new <see cref="ComPtr{T}"/> instance from a raw pointer and increments the ref count.</summary>
/// <param name="other">The raw pointer to wrap.</param>
public ComPtr(T* other)
{
ptr_ = other;
InternalAddRef();
}
/// <summary>Creates a new <see cref="ComPtr{T}"/> instance from a second one and increments the ref count.</summary>
/// <param name="other">The other <see cref="ComPtr{T}"/> instance to copy.</param>
public ComPtr(ComPtr<T> other)
{
ptr_ = other.ptr_;
InternalAddRef();
}
/// <summary>
/// Releases a target COM object, if it's not <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The type of COM objects to work with.</typeparam>
/// <param name="other">The COM object to potentially release.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(T* other)
public static void Release<T>(T* other)
where T : unmanaged
{
if (other is not null)
{
@@ -54,10 +36,12 @@ internal unsafe struct ComPtr<T> : IDisposable
/// <summary>
/// Copies a given COM object into a target destination, incrementing reference counts where needed.
/// </summary>
/// <typeparam name="T">The type of COM objects to work with.</typeparam>
/// <param name="obj">The input COM object to copy.</param>
/// <param name="other">The target destination for the COM object.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyTo(T* obj, ref T* other)
public static void CopyTo<T>(T* obj, ref T* other)
where T : unmanaged
{
// We specifically need to increment the reference count on the source object before releasing the
// target object, to avoid issues in case source and destination were pointers to the same object
@@ -84,15 +68,43 @@ internal unsafe struct ComPtr<T> : IDisposable
/// <summary>
/// Writes a given COM object into a target (assumed uninitialized) destination, incrementing reference counts where needed.
/// </summary>
/// <typeparam name="T">The type of COM objects to work with.</typeparam>
/// <param name="obj">The input COM object to copy.</param>
/// <param name="other">The target destination for the COM object.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteTo(T* obj, out T* other)
public static void WriteTo<T>(T* obj, out T* other)
where T : unmanaged
{
_ = ((IUnknown*)obj)->AddRef();
other = obj;
}
}
/// <summary>A type that allows working with pointers to COM objects more securely.</summary>
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
/// <remarks>While this type is not marked as <see langword="ref"/> so that it can also be used in fields, make sure to keep the reference counts properly tracked if you do store <see cref="ComPtr{T}"/> instances on the heap.</remarks>
internal unsafe struct ComPtr<T> : IDisposable
where T : unmanaged
{
/// <summary>The raw pointer to a COM object, if existing.</summary>
private T* ptr_;
/// <summary>Creates a new <see cref="ComPtr{T}"/> instance from a raw pointer and increments the ref count.</summary>
/// <param name="other">The raw pointer to wrap.</param>
public ComPtr(T* other)
{
ptr_ = other;
InternalAddRef();
}
/// <summary>Creates a new <see cref="ComPtr{T}"/> instance from a second one and increments the ref count.</summary>
/// <param name="other">The other <see cref="ComPtr{T}"/> instance to copy.</param>
public ComPtr(ComPtr<T> other)
{
ptr_ = other.ptr_;
InternalAddRef();
}
/// <summary>Converts a raw pointer to a new <see cref="ComPtr{T}"/> instance and increments the ref count.</summary>
/// <param name="other">The raw pointer to wrap.</param>