using System;
using System.Runtime.InteropServices;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if !NET6_0_OR_GREATER
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
#endif
namespace ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
///
partial struct ResourceTextureManager
{
///
/// The implementation for ID2D1ResourceTextureManagerInternal.
///
private unsafe static class ID2D1ResourceTextureManagerInternalMethods
{
#if !NET6_0_OR_GREATER
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int SetEffectContextDelegate(ResourceTextureManager* @this, ID2D1EffectContext* effectContext);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int GetResourceTextureDelegate(ResourceTextureManager* @this, ID2D1ResourceTexture** resourceTexture);
///
/// A cached instance wrapping .
///
public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
///
/// A cached instance wrapping .
///
public static readonly AddRefDelegate AddRefWrapper = AddRef;
///
/// A cached instance wrapping .
///
public static readonly ReleaseDelegate ReleaseWrapper = Release;
///
/// A cached instance wrapping .
///
public static readonly SetEffectContextDelegate SetEffectContextWrapper = SetEffectContext;
///
/// A cached instance wrapping .
///
public static readonly GetResourceTextureDelegate GetResourceTextureWrapper = GetResourceTexture;
#endif
///
[UnmanagedCallersOnly]
public static int QueryInterface(ResourceTextureManager* @this, Guid* riid, void** ppvObject)
{
@this = (ResourceTextureManager*)&((void**)@this)[-1];
return @this->QueryInterface(riid, ppvObject);
}
///
[UnmanagedCallersOnly]
public static uint AddRef(ResourceTextureManager* @this)
{
@this = (ResourceTextureManager*)&((void**)@this)[-1];
return @this->AddRef();
}
///
[UnmanagedCallersOnly]
public static uint Release(ResourceTextureManager* @this)
{
@this = (ResourceTextureManager*)&((void**)@this)[-1];
return @this->Release();
}
///
[UnmanagedCallersOnly]
public static int SetEffectContext(ResourceTextureManager* @this, ID2D1EffectContext* effectContext)
{
@this = (ResourceTextureManager*)&((void**)@this)[-1];
if (effectContext is null)
{
return E.E_POINTER;
}
lock (@this->lockHandle.Target!)
{
// If this is the first time the method is called, just store the context
if (@this->d2D1EffectContext is null)
{
effectContext->AddRef();
@this->d2D1EffectContext = effectContext;
return S.S_OK;
}
// Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture
// manager to be shared across effects. If the resource cannot be shared, it will just
// return an error when effect is actually set, with ID2D1DrawInfo::SetResourceTexture.
return S.S_FALSE;
}
}
///
[UnmanagedCallersOnly]
public static int GetResourceTexture(ResourceTextureManager* @this, ID2D1ResourceTexture** resourceTexture)
{
@this = (ResourceTextureManager*)&((void**)@this)[-1];
if (*resourceTexture is not null)
{
return E.E_POINTER;
}
lock (@this->lockHandle.Target!)
{
// If the effect context is null, it means Initialize has not been called yet
if (@this->d2D1EffectContext is null)
{
return E.E_NOT_VALID_STATE;
}
// If the texture has already been created, just return it
if (@this->d2D1ResourceTexture is not null)
{
@this->d2D1ResourceTexture->AddRef();
*resourceTexture = @this->d2D1ResourceTexture;
return S.S_OK;
}
// If the data is null at this point, it means CreateResourceTexture has not been called yet
if (@this->data is null)
{
return E.E_NOT_VALID_STATE;
}
// Create the resource now, as it hasn't been created yet
int result = @this->d2D1EffectContext->CreateResourceTexture(
resourceId: @this->resourceId,
resourceTextureProperties: &@this->resourceTextureProperties,
data: @this->data,
strides: @this->strides,
dataSize: @this->dataSize,
resourceTexture: resourceTexture);
// If creation was successful, release the buffered data. Going forwards,
// the resource texture will be used directly for all updates requested.
if (result == S.S_OK)
{
NativeMemory.Free(@this->resourceTextureProperties.extents);
NativeMemory.Free(@this->resourceTextureProperties.extendModes);
NativeMemory.Free(@this->data);
NativeMemory.Free(@this->strides);
// Reset the stored pointers to avoid double frees from Release()
@this->resourceTextureProperties.extents = null;
@this->resourceTextureProperties.extendModes = null;
@this->data = null;
@this->strides = null;
}
return result;
}
}
}
}