using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; #endif namespace ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; /// /// An implementation of the and interfaces. /// internal unsafe partial struct D2D1ResourceTextureManagerImpl { #if !NET6_0_OR_GREATER /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int QueryInterfaceDelegate(D2D1ResourceTextureManagerImpl* @this, Guid* riid, void** ppvObject); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint AddRefDelegate(D2D1ResourceTextureManagerImpl* @this); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint ReleaseDelegate(D2D1ResourceTextureManagerImpl* @this); #endif /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1ResourceTextureManager = InitVtblForID2D1ResourceTextureManagerAndID2D1ResourceTextureManagerInternal(); /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1ResourceTextureManagerInternal = &VtblForID2D1ResourceTextureManager[5]; /// /// Initializes the combined vtable for and . /// /// The combined vtable for and . private static void** InitVtblForID2D1ResourceTextureManagerAndID2D1ResourceTextureManagerInternal() { void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1ResourceTextureManagerImpl), sizeof(void*) * 10); // ID2D1ResourceTextureManager #if NET6_0_OR_GREATER lpVtbl[0] = (delegate* unmanaged)&ID2D1ResourceTextureManagerMethods.QueryInterface; lpVtbl[1] = (delegate* unmanaged)&ID2D1ResourceTextureManagerMethods.AddRef; lpVtbl[2] = (delegate* unmanaged)&ID2D1ResourceTextureManagerMethods.Release; lpVtbl[3] = (delegate* unmanaged)&ID2D1ResourceTextureManagerMethods.Initialize; lpVtbl[4] = (delegate* unmanaged)&ID2D1ResourceTextureManagerMethods.Update; #else lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerMethods.QueryInterfaceWrapper); lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerMethods.AddRefWrapper); lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerMethods.ReleaseWrapper); lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerMethods.InitializeWrapper); lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerMethods.UpdateWrapper); #endif // ID2D1ResourceTextureManagerInternal #if NET6_0_OR_GREATER lpVtbl[5 + 0] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.QueryInterface; lpVtbl[5 + 1] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.AddRef; lpVtbl[5 + 2] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.Release; lpVtbl[5 + 3] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.Initialize; lpVtbl[5 + 4] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.GetResourceTexture; #else lpVtbl[5 + 0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.QueryInterfaceWrapper); lpVtbl[5 + 1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.AddRefWrapper); lpVtbl[5 + 2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.ReleaseWrapper); lpVtbl[5 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.InitializeWrapper); lpVtbl[5 + 4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.GetResourceTextureWrapper); #endif return lpVtbl; } /// /// The vtable pointer for the current instance, for . /// private void** lpVtblForID2D1ResourceTextureManager; /// /// The vtable pointer for the current instance, for . /// private void** lpVtblForID2D1ResourceTextureManagerInternal; /// /// The current reference count for the object (from ). /// private volatile int referenceCount; /// /// Indicates whether the current instance requires multithread support to work safely. /// /// /// This is used when the object is wrapped in a RCW with a finalizer, which could run at any time. /// In that case, multithread support is required, and using a single thread factory is an error. /// private int requiresMultithread; /// /// The object currently wrapped by the current instance. /// private ID2D1EffectContext* d2D1EffectContext; /// /// The instance used to lock accesses to and . /// private ID2D1Multithread* d2D1Multithread; /// /// The object, if one was created already. /// private ID2D1ResourceTexture* d2D1ResourceTexture; /// /// The to identify the resource, if available. /// private Guid* resourceId; /// /// The for the resource texture. /// /// /// If is , it has not been set. /// private D2D1_RESOURCE_TEXTURE_PROPERTIES resourceTextureProperties; /// /// The texture data, if available. /// /// /// Once the texture has been created, this will no longer be necessary. /// private byte* data; /// /// An optional pointer to the stride to advance through the resource texture, according to dimension. /// private uint* strides; /// /// The size, in bytes, of the data. /// private uint dataSize; /// /// A to a dummy to use for locking. /// private GCHandle lockHandle; /// /// The expected dimensions for the slot the manager has been assigned to in an effect. /// /// If 0, it has not been set, so it can be ignored. private uint expectedDimensions; /// /// The factory method for instances. /// /// The resulting resource texture manager instance. /// Indicates whether the current instance requires multithread support to work safely. public static void Factory(D2D1ResourceTextureManagerImpl** resourceTextureManager, bool requiresMultithread) { D2D1ResourceTextureManagerImpl* @this = (D2D1ResourceTextureManagerImpl*)NativeMemory.Alloc((nuint)sizeof(D2D1ResourceTextureManagerImpl)); *@this = default; @this->lpVtblForID2D1ResourceTextureManager = VtblForID2D1ResourceTextureManager; @this->lpVtblForID2D1ResourceTextureManagerInternal = VtblForID2D1ResourceTextureManagerInternal; @this->referenceCount = 1; @this->d2D1EffectContext = null; @this->d2D1Multithread = null; @this->d2D1ResourceTexture = null; @this->requiresMultithread = requiresMultithread ? 1 : 0; @this->resourceId = null; @this->resourceTextureProperties = default; @this->data = null; @this->strides = null; @this->dataSize = 0; @this->lockHandle = GCHandle.Alloc(new object()); @this->expectedDimensions = 0; *resourceTextureManager = @this; } /// public int QueryInterface(Guid* riid, void** ppvObject) { if (ppvObject is null) { return E.E_POINTER; } // ID2D1ResourceTextureManager if (riid->Equals(Windows.__uuidof()) || riid->Equals(ID2D1ResourceTextureManager.Guid)) { _ = Interlocked.Increment(ref this.referenceCount); *ppvObject = Unsafe.AsPointer(ref this); return S.S_OK; } // ID2D1ResourceTextureManagerInternal if (riid->Equals(ID2D1ResourceTextureManagerInternal.Guid)) { _ = Interlocked.Increment(ref this.referenceCount); *ppvObject = (void**)Unsafe.AsPointer(ref this) + 1; return S.S_OK; } *ppvObject = null; return E.E_NOINTERFACE; } /// public uint AddRef() { return (uint)Interlocked.Increment(ref this.referenceCount); } /// public uint Release() { uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount); if (referenceCount == 0) { lock (this.lockHandle.Target!) { // Also synchronize on the ID2D1Multithread instance before touching D2D objects if (this.d2D1Multithread 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(); // Release the resource too if it has been created 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 // 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.d2D1Multithread->Leave(); _ = this.d2D1Multithread->Release(); } NativeMemory.Free(this.resourceId); NativeMemory.Free(this.resourceTextureProperties.extents); NativeMemory.Free(this.resourceTextureProperties.extendModes); NativeMemory.Free(this.data); NativeMemory.Free(this.strides); } this.lockHandle.Free(); NativeMemory.Free(Unsafe.AsPointer(ref this)); } return referenceCount; } }