Use ComPtr<T> for all fields of native objects

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-21 12:07:14 +01:00
1 親 423ee790ca
コミット 265de8d1ea
13ファイル変更
+96 -117

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

@@ -69,10 +69,10 @@ partial struct PixelShaderEffect
{
default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectsCount, @this->GetGlobals().InputCount, nameof(inputRectsCount));
if (@this->d2D1TransformMapper is not null)
if (@this->d2D1TransformMapper.Get() is not null)
{
// Forward to the current ID2D1TransformMapper instance
@this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert();
@this->d2D1TransformMapper.Get()->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert();
}
else
{
@@ -117,7 +117,7 @@ partial struct PixelShaderEffect
{
default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectCount, @this->GetGlobals().InputCount, nameof(inputRectCount));
if (@this->d2D1TransformMapper is not null)
if (@this->d2D1TransformMapper.Get() is not null)
{
using ComPtr<D2D1DrawInfoUpdateContextImpl> d2D1DrawInfoUpdateContext = default;
@@ -126,10 +126,10 @@ partial struct PixelShaderEffect
drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(),
constantBuffer: @this->constantBuffer,
constantBufferSize: @this->GetGlobals().ConstantBufferSize,
d2D1DrawInfo: @this->d2D1DrawInfo).Assert();
d2D1DrawInfo: @this->d2D1DrawInfo.Get()).Assert();
// Forward the call to the input ID2D1TransformMapper instance
HRESULT hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect(
HRESULT hresult = @this->d2D1TransformMapper.Get()->MapInputRectsToOutputRect(
updateContext: (ID2D1DrawInfoUpdateContext*)d2D1DrawInfoUpdateContext.Get(),
inputRects: inputRects,
inputOpaqueSubRects: inputOpaqueSubRects,
@@ -209,10 +209,10 @@ partial struct PixelShaderEffect
{
default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual((int)inputIndex, @this->GetGlobals().InputCount, nameof(inputIndex));
if (@this->d2D1TransformMapper is not null)
if (@this->d2D1TransformMapper.Get() is not null)
{
// Forward to the current ID2D1TransformMapper instance
@this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert();
@this->d2D1TransformMapper.Get()->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert();
}
else
{
@@ -248,7 +248,7 @@ partial struct PixelShaderEffect
try
{
// Store the new ID2D1DrawInfo object
ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo);
@this->d2D1DrawInfo.Attach(new ComPtr<ID2D1DrawInfo>(drawInfo).Get());
Guid shaderId = @this->GetGlobals().EffectId;
D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions;
@@ -70,7 +70,7 @@ internal unsafe partial struct PixelShaderEffect
transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert();
// Store the new ID2D1EffectContext object
ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext);
@this->d2D1EffectContext.Attach(new ComPtr<ID2D1EffectContext>(effectContext).Get());
return S.S_OK;
}
@@ -99,7 +99,7 @@ internal unsafe partial struct PixelShaderEffect
// First, set the constant buffer, if available
if (@this->constantBuffer is not null)
{
@this->d2D1DrawInfo->SetPixelShaderConstantBuffer(
@this->d2D1DrawInfo.Get()->SetPixelShaderConstantBuffer(
buffer: @this->constantBuffer,
bufferCount: (uint)@this->GetGlobals().ConstantBufferSize).Assert();
}
@@ -131,7 +131,7 @@ internal unsafe partial struct PixelShaderEffect
ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i];
// Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use
@this->d2D1DrawInfo->SetResourceTexture(
@this->d2D1DrawInfo.Get()->SetResourceTexture(
textureIndex: (uint)resourceTextureDescription.Index,
resourceTexture: d2D1ResourceTexture.Get()).Assert();
}
+4 -14
ファイルの表示
@@ -107,14 +107,7 @@ unsafe partial struct PixelShaderEffect
default(ArgumentNullException).ThrowIfNull(data);
default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize));
if (@this->d2D1TransformMapper is null)
{
*(void**)data = null;
}
else
{
((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof<ID2D1TransformMapper>(), (void**)data).Assert();
}
@this->d2D1TransformMapper.CopyTo((ID2D1TransformMapper**)data).Assert();
if (actualSize is not null)
{
@@ -150,11 +143,8 @@ unsafe partial struct PixelShaderEffect
// Check that the input object implements ID2D1TransformMapper
unknown.CopyTo(transformMapper.GetAddressOf()).Assert();
// If there's already an existing manager, release it
ComPtr.Dispose(@this->d2D1TransformMapper);
// Store the transform mapper manager into the effect
@this->d2D1TransformMapper = transformMapper.Detach();
@this->d2D1TransformMapper.Attach(transformMapper.Detach());
return S.S_OK;
}
@@ -226,7 +216,7 @@ unsafe partial struct PixelShaderEffect
unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf()).Assert();
// Initialize the resource texture manager, if an effect context is available
if (this.d2D1EffectContext is not null)
if (this.d2D1EffectContext.Get() is not null)
{
uint dimensions = (uint)GetGlobals().ResourceTextureDescriptions.Span[index].Dimensions;
@@ -235,7 +225,7 @@ unsafe partial struct PixelShaderEffect
// enable sharing resource texture managers across different source textures and effects), or
// E_INVALIDARG if the manager has already been initialized through the public interface,
// and the stored dimensions for that don't match the ones for this resource texture index.
resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions).Assert();
resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext.Get(), &dimensions).Assert();
}
// Store the resource texture manager into the buffer
+9 -9
ファイルの表示
@@ -80,17 +80,17 @@ internal unsafe partial struct PixelShaderEffect
/// <summary>
/// The <see cref="ID2D1TransformMapper"/> instance to use, if any.
/// </summary>
private ID2D1TransformMapper* d2D1TransformMapper;
private ComPtr<ID2D1TransformMapper> d2D1TransformMapper;
/// <summary>
/// The <see cref="ID2D1DrawInfo"/> instance currently in use.
/// </summary>
private ID2D1DrawInfo* d2D1DrawInfo;
private ComPtr<ID2D1DrawInfo> d2D1DrawInfo;
/// <summary>
/// The <see cref="ID2D1EffectContext"/> instance currently in use.
/// </summary>
private ID2D1EffectContext* d2D1EffectContext;
private ComPtr<ID2D1EffectContext> d2D1EffectContext;
/// <summary>
/// The resource texture managers for the current instance.
@@ -118,9 +118,9 @@ internal unsafe partial struct PixelShaderEffect
@this->referenceCount = 1;
@this->globalsHandle = globalsHandle;
@this->constantBuffer = null;
@this->d2D1TransformMapper = null;
@this->d2D1DrawInfo = null;
@this->d2D1EffectContext = null;
@this->d2D1TransformMapper = default;
@this->d2D1DrawInfo = default;
@this->d2D1EffectContext = default;
@this->resourceTextureManagerBuffer = default;
*effectImpl = (IUnknown*)@this;
@@ -197,9 +197,9 @@ internal unsafe partial struct PixelShaderEffect
NativeMemory.Free(this.constantBuffer);
ComPtr.Dispose(this.d2D1TransformMapper);
ComPtr.Dispose(this.d2D1DrawInfo);
ComPtr.Dispose(this.d2D1EffectContext);
this.d2D1TransformMapper.Dispose();
this.d2D1DrawInfo.Dispose();
this.d2D1EffectContext.Dispose();
// Retrieve all possible resource texture managers in use and release the ones that had been
// assigned (from one of the property bindings). We just try to dispose all of them here and
@@ -19,7 +19,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
lock (@this->lockHandle.Target!)
{
// If the method has already been called, fail
if (@this->d2D1ResourceTexture is not null ||
if (@this->d2D1ResourceTexture.Get() is not null ||
@this->data is not null)
{
return E.E_NOT_VALID_STATE;
@@ -35,20 +35,20 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
}
// If the method has already been called, just forward the call
if (@this->d2D1EffectContext is not null)
if (@this->d2D1EffectContext.Get() is not null)
{
@this->d2D1Multithread->Enter();
@this->d2D1Multithread.Get()->Enter();
// Create the resource after taking a D2D lock
int hresult = @this->d2D1EffectContext->CreateResourceTexture(
int hresult = @this->d2D1EffectContext.Get()->CreateResourceTexture(
resourceId: resourceId,
resourceTextureProperties: resourceTextureProperties,
data: data,
strides: strides,
dataSize: dataSize,
resourceTexture: &@this->d2D1ResourceTexture);
resourceTexture: @this->d2D1ResourceTexture.GetAddressOf());
@this->d2D1Multithread->Leave();
@this->d2D1Multithread.Get()->Leave();
return hresult;
}
@@ -76,14 +76,14 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
{
lock (@this->lockHandle.Target!)
{
if (@this->d2D1ResourceTexture is null &&
if (@this->d2D1ResourceTexture.Get() is null &&
@this->data is null)
{
return E.E_NOT_VALID_STATE;
}
// If no resource texture already exists, update the staging buffer
if (@this->d2D1ResourceTexture is null)
if (@this->d2D1ResourceTexture.Get() is null)
{
return ID2D1ResourceTextureManagerMethods.UpdateWithStagingBuffer(
@this: @this,
@@ -103,10 +103,10 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
// checked for this while taking the instance lock). So we can now take the D2D
// lock and update the texture directly. We need to take this when we're not also
// taking the instance lock, or we might trigger an AB/BA deadlock across threads.
@this->d2D1Multithread->Enter();
@this->d2D1Multithread.Get()->Enter();
// Take a D2D lock here too to ensure thread safety
int hresult = @this->d2D1ResourceTexture->Update(
int hresult = @this->d2D1ResourceTexture.Get()->Update(
minimumExtents: minimumExtents,
maximimumExtents: maximimumExtents,
strides: strides,
@@ -114,7 +114,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
data: data,
dataCount: dataCount);
@this->d2D1Multithread->Leave();
@this->d2D1Multithread.Get()->Leave();
return hresult;
}
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Shaders.Interop.Extensions;
using ComputeSharp.Win32;
@@ -70,7 +71,7 @@ partial struct D2D1ResourceTextureManagerImpl
// If this is the first time the method is called, just store the context.
// Before doing so, get an ID2D1Multithread instance, to ensure thread safety.
// For additional info, see docs on ID2D1EffectFactoryExtensions.GetD2D1Multithread.
if (@this->d2D1EffectContext is null)
if (@this->d2D1EffectContext.Get() is null)
{
using ComPtr<ID2D1Multithread> d2D1Multithread = default;
@@ -88,9 +89,9 @@ 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.WriteTo(effectContext, out @this->d2D1EffectContext);
@this->d2D1Multithread = d2D1Multithread.Detach();
// Also store the actual multithread object after storing the effect context.
@this->d2D1EffectContext.Attach(new ComPtr<ID2D1EffectContext>(effectContext).Get());
@this->d2D1Multithread.Attach(d2D1Multithread.Detach());
}
return hresult;
@@ -117,15 +118,15 @@ partial struct D2D1ResourceTextureManagerImpl
lock (@this->lockHandle.Target!)
{
// If the effect context is null, it means Initialize has not been called yet
if (@this->d2D1EffectContext is null)
if (@this->d2D1EffectContext.Get() is null)
{
return E.E_NOT_VALID_STATE;
}
// If the texture has already been created, just return it
if (@this->d2D1ResourceTexture is not null)
if (@this->d2D1ResourceTexture.Get() is not null)
{
ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture);
@this->d2D1ResourceTexture.CopyTo(resourceTexture).Assert();
return S.S_OK;
}
@@ -139,7 +140,7 @@ partial struct D2D1ResourceTextureManagerImpl
using ComPtr<ID2D1ResourceTexture> d2D1ResourceTexture = default;
// Create the resource now, as it hasn't been created yet
int result = @this->d2D1EffectContext->CreateResourceTexture(
int result = @this->d2D1EffectContext.Get()->CreateResourceTexture(
resourceId: @this->resourceId,
resourceTextureProperties: &@this->resourceTextureProperties,
data: @this->data,
@@ -74,17 +74,17 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
/// <summary>
/// The <see cref="ID2D1EffectContext"/> object currently wrapped by the current instance.
/// </summary>
private ID2D1EffectContext* d2D1EffectContext;
private ComPtr<ID2D1EffectContext> d2D1EffectContext;
/// <summary>
/// The <see cref="ID2D1Multithread"/> instance used to lock accesses to <see cref="d2D1EffectContext"/> and <see cref="d2D1ResourceTexture"/>.
/// </summary>
private ID2D1Multithread* d2D1Multithread;
private ComPtr<ID2D1Multithread> d2D1Multithread;
/// <summary>
/// The <see cref="ID2D1ResourceTexture"/> object, if one was created already.
/// </summary>
private ID2D1ResourceTexture* d2D1ResourceTexture;
private ComPtr<ID2D1ResourceTexture> d2D1ResourceTexture;
/// <summary>
/// The <see cref="Guid"/> to identify the resource, if available.
@@ -142,9 +142,9 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
@this->lpVtblForID2D1ResourceTextureManager = VtblForID2D1ResourceTextureManager;
@this->lpVtblForID2D1ResourceTextureManagerInternal = VtblForID2D1ResourceTextureManagerInternal;
@this->referenceCount = 1;
@this->d2D1EffectContext = null;
@this->d2D1Multithread = null;
@this->d2D1ResourceTexture = null;
@this->d2D1EffectContext = default;
@this->d2D1Multithread = default;
@this->d2D1ResourceTexture = default;
@this->requiresMultithread = requiresMultithread ? 1 : 0;
@this->resourceId = null;
@this->resourceTextureProperties = default;
@@ -211,24 +211,25 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
this.lockHandle.Free();
// Synchronize on the ID2D1Multithread instance before touching D2D objects
if (this.d2D1Multithread is not null)
if (this.d2D1Multithread.Get() is not null)
{
// Enter the lock, and then free the effect context. That is guaranteed to
// not be null here, as it is only ever set if ID2D1Multithread is retrieved.
this.d2D1Multithread->Enter();
this.d2D1Multithread.Get()->Enter();
// Release the resource too if it has been created
ComPtr.Dispose(this.d2D1ResourceTexture);
this.d2D1ResourceTexture.Dispose();
// 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
// been released. Releasing an effect context first, in case it was the last reference
// to it, will otherwise cause the Release() call on the resource texture to explode.
_ = this.d2D1EffectContext->Release();
this.d2D1EffectContext.Dispose();
this.d2D1Multithread->Leave();
this.d2D1Multithread.Get()->Leave();
_ = this.d2D1Multithread->Release();
// Finally release the multithread object after exiting the lock and releasing all other D2D objects
this.d2D1Multithread.Dispose();
}
NativeMemory.Free(this.resourceId);
@@ -42,7 +42,7 @@ partial struct D2D1DrawInfoUpdateContextImpl
return E.E_POINTER;
}
if (@this->d2D1DrawInfo is null)
if (@this->d2D1DrawInfo.Get() is null)
{
return RO.RO_E_CLOSED;
}
@@ -71,7 +71,7 @@ partial struct D2D1DrawInfoUpdateContextImpl
return E.E_NOT_VALID_STATE;
}
if (@this->d2D1DrawInfo is null)
if (@this->d2D1DrawInfo.Get() is null)
{
return RO.RO_E_CLOSED;
}
@@ -98,7 +98,7 @@ partial struct D2D1DrawInfoUpdateContextImpl
return E.E_INVALIDARG;
}
if (@this->d2D1DrawInfo is null)
if (@this->d2D1DrawInfo.Get() is null)
{
return RO.RO_E_CLOSED;
}
@@ -113,7 +113,7 @@ partial struct D2D1DrawInfoUpdateContextImpl
Buffer.MemoryCopy(buffer, @this->constantBuffer, bufferCount, bufferCount);
// Propagate it to the ID2D1DrawInfo object in use as well
return @this->d2D1DrawInfo->SetPixelShaderConstantBuffer(buffer, bufferCount);
return @this->d2D1DrawInfo.Get()->SetPixelShaderConstantBuffer(buffer, bufferCount);
}
}
}
@@ -10,7 +10,8 @@ unsafe partial struct D2D1DrawInfoUpdateContextImpl
/// <inheritdoc cref="ID2D1DrawInfoUpdateContextInternal.Close"/>
public int Close()
{
this.d2D1DrawInfo = null;
// No need to call Release on the ID2D1DrawInfo object (see notes in the Factory method)
this.d2D1DrawInfo = default;
return S.S_OK;
}
@@ -75,7 +75,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl
/// <summary>
/// The <see cref="ID2D1DrawInfo"/> instance currently in use.
/// </summary>
private ID2D1DrawInfo* d2D1DrawInfo;
private ComPtr<ID2D1DrawInfo> d2D1DrawInfo;
/// <summary>
/// The factory method for <see cref="D2D1DrawInfoUpdateContextImpl"/> instances.
@@ -109,7 +109,13 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl
@this->referenceCount = 1;
@this->constantBuffer = constantBuffer;
@this->constantBufferSize = constantBufferSize;
@this->d2D1DrawInfo = d2D1DrawInfo;
// This ID2D1DrawInfo manager instance is short lived, and the contract guarantees that it will not remain
// alive after the underlying ID2D1DrawInfo instance has been invalidated. As such, we can optimize the
// initialization by skipping the AddRef/Release calls on the ID2D1DrawInfo object, and simply storing the
// pointer to the object. When this instance is closed, that will just be set to null again.
@this->d2D1DrawInfo = default;
@this->d2D1DrawInfo.Attach(d2D1DrawInfo);
*drawInfoUpdateContext = @this;