From 5c310be61f5d1ab393f02b457a42acda765e8e76 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Feb 2023 02:06:53 +0100 Subject: [PATCH] Simplify code in ICanvasEffect methods marshalling --- .../PixelShaderEffect{T}.ICanvasEffect.cs | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs index fb2f8f76..8cf7ff40 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasEffect.cs @@ -47,8 +47,8 @@ unsafe partial class PixelShaderEffect resourceCreatorWithDpi.GetAddressOf(), canvasImageInterop.GetAddressOf()); - uint valueCount = 0; - Rect* valueElements = null; + uint valueCount; + Rect* valueElements; Win2D.GetInvalidRectanglesForICanvasImageInterop( resourceCreator: resourceCreatorWithDpi.Get(), @@ -56,23 +56,11 @@ unsafe partial class PixelShaderEffect 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; + // Copy the data back from the COM array that Win2D created and returned. + // If no items were returned, this will automatically return an empty array. + return new Span(valueElements, checked((int)valueCount)).ToArray(); } finally {