using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using Windows.Graphics.Effects; using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS; using Win32 = TerraFX.Interop.Windows.Windows; namespace ComputeSharp.D2D1.Uwp; /// unsafe partial class PixelShaderEffect { /// /// Gets the marshalled value for . /// /// The index of the source to get or set. /// The marshalled value for . private IGraphicsEffectSource? GetSource(int index) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { return this.d2D1Effect.Get() switch { not null => GetD2DInput(index), _ => Sources.Storage[index].GetWrapper() }; } } /// /// Sets the marshalled value for . /// /// The value to set for . /// The index of the source to get or set. private void SetSource(IGraphicsEffectSource? value, int index) { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); lock (this.lockObject) { ref SourceReference source = ref Sources.Storage[index]; if (this.d2D1Effect.Get() is not null) { const WIN2D_GET_D2D_IMAGE_FLAGS flags = WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION | WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS | WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE; // The WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE flag is passed, so we just let the effect // unrealize in case of failure and take no other action (including throwing) if that happens. _ = SetD2DInput(index, value, flags, targetDpi: 0.0f, d2D1DeviceContext: null); } else { source.SetWrapper(value); } } } /// This method assumes a lock is taken and the effect is already realized. /// private IGraphicsEffectSource? GetD2DInput(int index) { using ComPtr d2D1Image = default; // Get the ID2D1Image input from the effect this.d2D1Effect.Get()->GetInput( index: (uint)index, input: d2D1Image.GetAddressOf()); ref SourceReference sourceReference = ref Sources.Storage[index]; // Check whether the input had a DPI compensation effect added to it, and skip it if so if (sourceReference.HasDpiCompensationEffect) { // Since a realized effect is the only source of authority for the effect state, we also // need to check that the actual source is in fact the same that was previous set by the // managed wrapper. If it is, we get the input and return that (ie. the underlying image). // If not, we just return it directly and reset the locally stored DPI compensation effect. if (d2D1Image.Get()->IsSameInstance(sourceReference.GetDpiCompensationEffect())) { sourceReference.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf()); } else { sourceReference.SetDpiCompensationEffect(null); } } // Get or create a wrapper for the input image return sourceReference.GetOrCreateWrapper(null, d2D1Image.Get()); } /// The index of the source to get or set. /// The value to set for . /// The flags for . /// The target DPI for the realized image, if present. /// The input object to use, if present. /// Whether the input was set successfully or not. /// This method assumes a lock is taken and the effect is already realized. /// private bool SetD2DInput( int index, IGraphicsEffectSource? value, WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* d2D1DeviceContext) { using ComPtr d2D1Image = default; float realizedDpi = 0; if (value is not null) { using ComPtr canvasImageUnknown = default; // Get the underlying IUnknown object for the input source canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value)); using ComPtr canvasImageInterop = default; // Try to get the ICanvasImageInterop interface from the input source HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf()); if (!Win32.SUCCEEDED(hresult)) { // Only potentially handle E_NOINTERFACE, always throw in other cases if (hresult != E.E_NOINTERFACE) { Marshal.ThrowExceptionForHR(hresult); } // If unrealization isn't requested for failures, just throw if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) { ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop)."); } Unrealize(value, index); return false; } using ComPtr sourceCanvasDevice = default; // Get the canvas device the source is realized on, if any canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert(); // If a device was retrieved, it must be the same as the input one. If the source has no device, // that is fine, as it just means that its underlying D2D image has not been realized yet. if (sourceCanvasDevice.Get() is not null && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get())) { if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) { ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device."); } Unrealize(value, index); return false; } // Try to realize the image from the input source (this will recurse through the effect graph) hresult = canvasImageInterop.Get()->GetD2DImage( device: this.canvasDevice.Get(), deviceContext: d2D1DeviceContext, flags: flags, targetDpi: targetDpi, realizeDpi: &realizedDpi, ppImage: d2D1Image.GetAddressOf()); // Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) { hresult.Assert(); } // If no realized image is available, unrealize the effect if (d2D1Image.Get() is null) { Unrealize(value, index); return false; } } else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) { // If the source is null and this is not explicitly allowed, the invocation is not valid ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null."); } // Save the managed wrapper and realized image Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get()); // TODO: handle DPI compensation // Set the actual effect input this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get()); return true; } }