From 26a75e68cf9165bc71c826fe31b50927da75c851 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Dec 2022 15:26:42 +0100 Subject: [PATCH] Implement ICanvasEffect support in PixelShaderEffect --- .../PixelShaderEffect{T}.ICanvasEffect.cs | 210 +++++++++++++++++- .../PixelShaderEffect{T}.ICanvasImage.cs | 20 +- 2 files changed, 209 insertions(+), 21 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs index 752418cc..fb2f8f76 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs @@ -1,44 +1,232 @@ using System; -using Microsoft.Graphics.Canvas; -using Microsoft.Graphics.Canvas.Effects; +using System.Buffers; +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.Interop; +using TerraFX.Interop.Windows; using Windows.Foundation; +using ICanvasEffect = Microsoft.Graphics.Canvas.Effects.ICanvasEffect; +using ICanvasResourceCreatorWithDpi = Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi; namespace ComputeSharp.D2D1.Uwp; /// -partial class PixelShaderEffect +unsafe partial class PixelShaderEffect { /// - void ICanvasEffect.InvalidateSourceRectangle(ICanvasResourceCreatorWithDpi resourceCreator, uint sourceIndex, Rect invalidRectangle) + public void InvalidateSourceRectangle(ICanvasResourceCreatorWithDpi resourceCreator, uint sourceIndex, Rect invalidRectangle) { - throw new NotSupportedException("ICanvasEffect.InvalidateSourceRectangle is not currently supported."); + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + using ComPtr resourceCreatorWithDpi = default; + using ComPtr canvasImageInterop = default; + + GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects( + resourceCreator, + resourceCreatorWithDpi.GetAddressOf(), + canvasImageInterop.GetAddressOf()); + + Win2D.InvalidateSourceRectangleForICanvasImageInterop( + resourceCreator: resourceCreatorWithDpi.Get(), + image: canvasImageInterop.Get(), + sourceIndex: sourceIndex, + invalidRectangle: &invalidRectangle).Assert(); } /// - Rect[] ICanvasEffect.GetInvalidRectangles(ICanvasResourceCreatorWithDpi resourceCreator) + public Rect[] GetInvalidRectangles(ICanvasResourceCreatorWithDpi resourceCreator) { - throw new NotSupportedException("ICanvasEffect.GetInvalidRectangles is not currently supported."); + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + using ComPtr resourceCreatorWithDpi = default; + using ComPtr canvasImageInterop = default; + + GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects( + resourceCreator, + resourceCreatorWithDpi.GetAddressOf(), + canvasImageInterop.GetAddressOf()); + + uint valueCount = 0; + Rect* valueElements = null; + + Win2D.GetInvalidRectanglesForICanvasImageInterop( + resourceCreator: resourceCreatorWithDpi.Get(), + image: canvasImageInterop.Get(), + valueCount: &valueCount, + valueElements: &valueElements).Assert(); + + // If no rectangles have been returned, just use an empty array + if (valueCount == 0 && valueElements is null) + { + return Array.Empty(); + } + + try + { + Rect[] result = new Rect[valueCount]; + + // Copy the data back from the COM array that Win2D created and returned + fixed (Rect* pResult = result) + { + Buffer.MemoryCopy(valueElements, pResult, valueCount * sizeof(Rect), valueCount * sizeof(Rect)); + } + + return result; + } + finally + { + // Make sure to release the callee-allocated COM array + Marshal.FreeCoTaskMem((IntPtr)valueElements); + } } /// - Rect ICanvasEffect.GetRequiredSourceRectangle( + public Rect GetRequiredSourceRectangle( ICanvasResourceCreatorWithDpi resourceCreator, Rect outputRectangle, ICanvasEffect sourceEffect, uint sourceIndex, Rect sourceBounds) { - throw new NotSupportedException("ICanvasEffect.GetRequiredSourceRectangle is not currently supported."); + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + using ComPtr resourceCreatorWithDpi = default; + using ComPtr canvasImageInterop = default; + + GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects( + resourceCreator, + resourceCreatorWithDpi.GetAddressOf(), + canvasImageInterop.GetAddressOf()); + + Rect result; + + using ComPtr canvasEffectUnknown = default; + using ComPtr canvasEffectAbi = default; + + // Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface + canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffect)); + canvasEffectUnknown.CopyTo(canvasEffectAbi.GetAddressOf()).Assert(); + + Win2D.GetRequiredSourceRectanglesForICanvasImageInterop( + resourceCreator: resourceCreatorWithDpi.Get(), + image: canvasImageInterop.Get(), + outputRectangle: &outputRectangle, + sourceEffectCount: 1, + sourceEffects: canvasEffectAbi.GetAddressOf(), + sourceIndexCount: 1, + sourceIndices: &sourceIndex, + sourceBoundsCount: 1, + sourceBounds: &sourceBounds, + valueCount: 1, + valueElements: &result).Assert(); + + return result; } /// - Rect[] ICanvasEffect.GetRequiredSourceRectangles( + public Rect[] GetRequiredSourceRectangles( ICanvasResourceCreatorWithDpi resourceCreator, Rect outputRectangle, ICanvasEffect[] sourceEffects, uint[] sourceIndices, Rect[] sourceBounds) { - throw new NotSupportedException("ICanvasEffect.GetRequiredSourceRectangles is not currently supported."); + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + using ComPtr resourceCreatorWithDpi = default; + using ComPtr canvasImageInterop = default; + + GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects( + resourceCreator, + resourceCreatorWithDpi.GetAddressOf(), + canvasImageInterop.GetAddressOf()); + + // The array optionally rented from the pool is of IntPtr types to reduce the number of generic + // instantiations. This increases the chances of the pooled arrays being shared with other code. + IntPtr[]? canvasEffectsAbiArray = null; + + // Create the span and optional array of ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect objects + Span> canvasEffects = sourceEffects.Length <= 16 + ? stackalloc ComPtr[16] + : MemoryMarshal.Cast>( + canvasEffectsAbiArray = ArrayPool.Shared.Rent(sourceEffects.Length)); + + // Clear the span to ensure no false positives are accidentally disposed + canvasEffects.Clear(); + + try + { + // Get the underlying ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object for each input effect + for (int i = 0; i < sourceEffects.Length; i++) + { + using ComPtr canvasEffectUnknown = default; + + canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffects[i])); + canvasEffectUnknown.CopyTo(canvasEffects[i].GetAddressOf()).Assert(); + } + + Rect[] result = new Rect[sourceEffects.Length]; + + fixed (ComPtr* pCanvasEffects = canvasEffects) + fixed (uint* pSourceIndices = sourceIndices) + fixed (Rect* pSourceBounds = sourceBounds) + fixed (Rect* pResult = result) + { + Win2D.GetRequiredSourceRectanglesForICanvasImageInterop( + resourceCreator: resourceCreatorWithDpi.Get(), + image: canvasImageInterop.Get(), + outputRectangle: &outputRectangle, + sourceEffectCount: (uint)sourceEffects.Length, + sourceEffects: (ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect**)pCanvasEffects, + sourceIndexCount: (uint)sourceIndices.Length, + sourceIndices: pSourceIndices, + sourceBoundsCount: (uint)sourceBounds.Length, + sourceBounds: pSourceBounds, + valueCount: (uint)sourceEffects.Length, + valueElements: pResult).Assert(); + } + + // Only return the array to the pool if no exceptions have been thrown + if (canvasEffectsAbiArray is not null) + { + ArrayPool.Shared.Return(canvasEffectsAbiArray); + } + + return result; + } + finally + { + // Release all canvas effects that have been retrieved up until the exception + foreach (ref ComPtr canvasEffect in canvasEffects) + { + canvasEffect.Dispose(); + } + } + } + + /// + /// Gets the underlying and + /// objects from an input object and the current instance. + /// + /// The input object. + /// The resulting object for . + /// The resulting object for the current instance. + private void GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects( + ICanvasResourceCreatorWithDpi resourceCreator, + ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi** resourceCreatorWithDpi, + ICanvasImageInterop** canvasImageInterop) + { + using ComPtr resourceCreatorUnknown = default; + + // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface + resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); + resourceCreatorUnknown.CopyTo(resourceCreatorWithDpi).Assert(); + + using ComPtr canvasImageInteropUnknown = default; + + // Get the ICanvasImageInterop object from the current instance + canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); + canvasImageInteropUnknown.CopyTo(canvasImageInterop).Assert(); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs index 0b31ffe3..d0f24e97 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs @@ -30,25 +30,25 @@ unsafe partial class PixelShaderEffect { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - using ComPtr canvasResourceCreatorUnknown = default; + using ComPtr resourceCreatorUnknown = default; + using ComPtr resourceCreatorAbi = default; + + // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface + resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); + resourceCreatorUnknown.CopyTo(resourceCreatorAbi.GetAddressOf()).Assert(); + using ComPtr canvasImageInteropUnknown = default; - - // Get the IUnknown* references for the ICanvasResourceCreator and ICanvasImageInterop objects - canvasResourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); - canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); - - using ComPtr canvasResourceCreator = default; using ComPtr canvasImageInterop = default; - // Get the ICanvasResourceCreator* and ICanvasImageInterop* pointers - canvasResourceCreatorUnknown.CopyTo(canvasResourceCreator.GetAddressOf()).Assert(); + // Get the ICanvasImageInterop object from the current instance + canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert(); Rect bounds; // Forward the actual logic to Win2D to compute the image bounds (it needs the internal context) Win2D.GetBoundsForICanvasImageInterop( - resourceCreator: canvasResourceCreator.Get(), + resourceCreator: resourceCreatorAbi.Get(), image: canvasImageInterop.Get(), transform: transform, rect: &bounds).Assert();