Marshalling “EGLRenderResolutionScaleProperty” to ANGLE from C# using P/Invoke

前端 未结 2 1459
日久生厌
日久生厌 2021-01-24 03:59

I am trying to get ANGLE working in C# using P/Invoke. Basically, I am creating a simple 2D surface, and then passing that off to skia (using SkiaSharp). Everything is working a

2条回答
  •  执笔经年
    2021-01-24 04:39

    After doing a bit of searching, I found the best (and only) way to do this was to create a Windows Runtime Component.

    As I could not have a "hard" reference to the component - this library must work without ANGLE present. I opted for a small component with a C API so that I could just P/Invoke it when I needed it. This was my method in C++:

    void PropertySetInterop_AddSingle(ABI::Windows::Foundation::Collections::IPropertySet* propertySet, HSTRING key, float scale)
    {
        using namespace Microsoft::WRL;
        using namespace Microsoft::WRL::Wrappers;
        using namespace ABI::Windows::Foundation;
        using namespace ABI::Windows::Foundation::Collections;
    
        ComPtr propSet = propertySet;
        ComPtr> map;
        propSet.As(&map);
    
        ComPtr propValueFactory;
        GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), &propValueFactory);
    
        ComPtr valueInspectable;
        propValueFactory->CreateSingle(scale, &valueInspectable);
    
        boolean replaced;
        map->Insert(key, valueInspectable.Get(), &replaced);
    }
    

    I am using COM here so that I can use this in C/C++ code. My P/Invoke code is this:

    internal static class PropertySetInterop
    {
        public static void AddSingle(PropertySet properties, string key, float value)
        {
            PropertySetInterop_AddSingle(properties, key, value);
        }
    
        public static void AddSize(PropertySet properties, string key, float width, float height)
        {
            PropertySetInterop_AddSize(properties, key, width, height);
        }
    
        private const string libInterop = "SkiaSharp.Views.Interop.UWP.dll";
    
        [DllImport(libInterop)]
        private static extern void PropertySetInterop_AddSingle(
            [MarshalAs(UnmanagedType.IInspectable)] object properties,
            [MarshalAs(UnmanagedType.HString)] string key,
            float value);
    
        [DllImport(libInterop)]
        private static extern void PropertySetInterop_AddSize(
            [MarshalAs(UnmanagedType.IInspectable)] object properties,
            [MarshalAs(UnmanagedType.HString)] string key,
            float width, float height);
    }
    

提交回复
热议问题