Implement Win2D's ResourceManager::GetOrCreate logic

このコミットが含まれているのは:
Sergio Pedri committed 2022-12-22 10:56:03 -08:00
1 親 2611e9b1d1
コミット 4954bec5f4
3ファイル変更
+75 -4

検索条件に一致するファイルはありません

+6 -3
ファイルの表示
@@ -2,6 +2,7 @@ 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;
@@ -140,12 +141,14 @@ internal unsafe struct SourceReference : IDisposable
// If that's not the case, create a new wrapper if the input image is not null
if (d2D1Image is not null)
{
// m_wrapper = ResourceManager::GetOrCreate<TWrapper>(device, resource);
throw new NotImplementedException();
this.graphicsEffectSourceWrapper = ResourceManager.GetOrCreate(
device: canvasDevice,
resource: (IUnknown*)d2D1Image,
dpi: 0);
}
else
{
this.d2D1ImageSource.Dispose();
this.graphicsEffectSourceWrapper = null;
}
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
+68
ファイルの表示
@@ -0,0 +1,68 @@
using System;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
using Windows.Graphics.Effects;
namespace ComputeSharp.D2D1.Uwp.Helpers;
/// <summary>
/// A helper type to replicate Win2D's <c>ResourceManager</c> type.
/// </summary>
/// <remarks>
/// This type provides an entry point to use the Win2D <see cref="ICanvasFactoryNative"/> activation factory.
/// </remarks>
internal static class ResourceManager
{
/// <summary>
/// Gets or creates an <see cref="IGraphicsEffectSource"/> instance for a native resource.
/// </summary>
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
/// <param name="resource">The input native resource to create a wrapper for.</param>
/// <param name="dpi">The realization DPIs for <paramref name="resource"/></param>
/// <returns>The resulting <see cref="IGraphicsEffectSource"/> wrapper instance.</returns>
/// <exception cref="Exception">Thrown if the managed wrapper could not be retrieved.</exception>
public static unsafe IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
{
const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
fixed (char* pActivatableClassId = activatableClassId)
{
HSTRING_HEADER hStringHeaderActivatableClassId;
HSTRING hStringActivatableClass;
// Create a fast-pass HSTRING for "Microsoft.Graphics.Canvas.CanvasDevice"
WinRT.WindowsCreateStringReference(
sourceString: (ushort*)pActivatableClassId,
length: (uint)activatableClassId.Length,
hstringHeader: &hStringHeaderActivatableClassId,
@string: &hStringActivatableClass).Assert();
Guid canvasFactoryNativeId = typeof(ICanvasFactoryNative).GUID;
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
// Get the activation factory for CanvasDevice
WinRT.RoGetActivationFactory(
activatableClassId: hStringActivatableClass,
iid: &canvasFactoryNativeId,
factory: canvasFactoryNative.GetVoidAddressOf()).Assert();
using ComPtr<IUnknown> wrapperUnknown = default;
// Get or create a WinRT wrapper for the resource
canvasFactoryNative.Get()->GetOrCreate(
device: device,
resource: resource,
dpi: dpi,
wrapper: wrapperUnknown.GetVoidAddressOf()).Assert();
// Get the runtime-provided RCW for the resulting WinRT wrapper
object wrapper = Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get());
return (IGraphicsEffectSource)wrapper;
}
}
}
+1 -1
ファイルの表示
@@ -103,7 +103,7 @@ unsafe partial class PixelShaderEffect<T>
}
// Get or create a wrapper for the input image
return sourceReference.GetOrCreateWrapper(null, d2D1Image.Get());
return sourceReference.GetOrCreateWrapper(this.canvasDevice.Get(), d2D1Image.Get());
}
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>