using System; using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.D2D1.Uwp.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using Windows.Graphics.Effects; namespace ComputeSharp.D2D1.Uwp.Buffers; /// /// A wrapper type for an input and its underlying native D2D images. /// /// /// /// This type is meant to support these two common operations: /// /// Look up a D2D resource from a property of some other D2D interface. /// Get a managed wrapper from a D2D interface. /// /// With this, the generated D2D objects can be associated with each original managed wrapper. Similarly, /// when the other way around is used, this type can also store the generated wrapper for future use. /// /// /// This gives the following improvements: /// /// Improved performance for the common case of repeated calls using the same D2D instance. /// Maintaining a reference to the wrapper avoids it being repeatedly released and recreated. /// /// /// /// Just like with all other members of an effect, values in this object are considered authoritative when the /// effect is not realized, otherwise they just act as a cache to speedup the reverse managed wrapper lookups. /// /// /// For realized effects, tracks both the resource and its /// wrapper (and optional being the DPI compensation effect, /// if one is needed. For unrealized effects, the resource is and only the wrapper part is used. /// /// internal unsafe struct SourceReference : IDisposable { /// /// The produced by calling /// on the input managed wrapper used as source for the effect (ie. ). /// private ComPtr d2D1ImageSource; /// /// The for DPI compensation wrapping , if needed. /// private ComPtr d2D1EffectDpiCompensation; /// /// The instance being a managed wrapper for the current effect input. /// private IGraphicsEffectSource? graphicsEffectSourceWrapper; /// /// Gets whether or not this source ference has a DPI compensation effect. /// public readonly bool HasDpiCompensationEffect { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.d2D1EffectDpiCompensation.Get() is not null; } /// /// Gets the object for the DPI compensation effect used, if any. /// /// The object for the DPI compensation effect used, if any. [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ID2D1Effect* GetDpiCompensationEffect() { return this.d2D1EffectDpiCompensation.Get(); } /// /// Sets the DPI compensation effet being used, /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetDpiCompensationEffect(ID2D1Effect* d2D1Effect) { this.d2D1EffectDpiCompensation.Dispose(); if (d2D1Effect is not null) { this.d2D1EffectDpiCompensation = new ComPtr(d2D1Effect); } } /// /// Gets the instance being a managed wrapper for the current effect input. /// /// The instance being a managed wrapper for the current effect input. [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly IGraphicsEffectSource? GetWrapper() { return this.graphicsEffectSourceWrapper; } /// /// Sets the instance being a managed wrapper for the current effect input. /// /// The instance being a managed wrapper for the current effect input. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetWrapper(IGraphicsEffectSource? wrapper) { this.d2D1ImageSource.Dispose(); this.graphicsEffectSourceWrapper = wrapper; } /// /// Sets the wrapper and underlying realized resource for the current source reference. /// /// The instance being a managed wrapper for the current effect input. /// The underlying relized instance for . [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetWrapperAndResource(IGraphicsEffectSource? wrapper, ID2D1Image* d2D1Image) { this.d2D1ImageSource.Dispose(); this.d2D1ImageSource = new ComPtr(d2D1Image); this.graphicsEffectSourceWrapper = wrapper; } /// /// Gets or creates an wrapper for a given object. /// /// The input object the source effect is realized on. /// The input object to get or create a wrapper for. /// An wrapper for a given object. public IGraphicsEffectSource? GetOrCreateWrapper(ICanvasDevice* canvasDevice, ID2D1Image* d2D1Image) { // Check if there already is a matching managed wrapper for the input image if (!this.d2D1ImageSource.Get()->IsSameInstance(d2D1Image)) { // If that's not the case, create a new wrapper if the input image is not null if (d2D1Image is not null) { this.graphicsEffectSourceWrapper = ResourceManager.GetOrCreate( device: canvasDevice, resource: (IUnknown*)d2D1Image, dpi: 0); } else { this.graphicsEffectSourceWrapper = null; } this.d2D1ImageSource = new ComPtr(d2D1Image); } return this.graphicsEffectSourceWrapper; } /// /// Updates the underlying resource, if needed. /// This is used after a potential re-realization of a lazily bound effect. /// /// The input object. /// Whether the resource has changed or not. public bool UpdateResource(ID2D1Image* d2D1Image) { if (d2D1Image->IsSameInstance(this.d2D1ImageSource.Get())) { return false; } this.d2D1ImageSource.Dispose(); this.d2D1ImageSource = new ComPtr(d2D1Image); return true; } /// public void Dispose() { this.d2D1ImageSource.Dispose(); this.d2D1EffectDpiCompensation.Dispose(); this.graphicsEffectSourceWrapper = null; } }