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 and all that, but I am having a problem marshalling PropertySet
to the unmanaged code.
This bit works fine:
// the properties
var props = new PropertySet();
props.Add("EGLNativeWindowTypeProperty", swapChainPanel);
// the surface attributes
int[] surfaceAttrs = {
EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, EGL_TRUE,
EGL_NONE
};
// create the surface
surface = eglCreateWindowSurface(eglDisplay, eglConfig, props, surfaceAttrs);
My import looks like this:
[DllImport("libEGL.dll")]
public static extern IntPtr eglCreateWindowSurface(
IntPtr dpy,
IntPtr config,
[MarshalAs(UnmanagedType.IInspectable)] object win,
int[] attrib_list);
The problem comes in when I am trying to set my scaling for high resolution screens. This "should" work:
var scale = PropertyValue.CreateSingle(2);
props.Add("EGLRenderResolutionScaleProperty", scale);
It works when using C++, but not in C#. I think I have got it down to the fact that the actual value is not being marshalled correctly. This is because when debugging in ANGLE code, it dies over here:
ComPtr<ABI::Windows::Foundation::IPropertyValue> propertyValue;
ABI::Windows::Foundation::PropertyType propertyType;
// ...
result = propertyValue->get_Type(&propertyType);
https://github.com/Microsoft/angle/blob/54b1fd01f7fddcd7011d5a04e9259edace8a13da/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.cpp#L242
The runtime exception is:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Are there any tips, or solutions?
Here is all my code for reference: https://gist.github.com/mattleibow/eacb9c9e87f306b218d99c713c532a82
Original ANGLE issue: https://github.com/Microsoft/angle/issues/89
EDIT
After further investigation, I found that for some reason PropertyValue
in C# is different to the same type in C++ (via Windows Runtime Component).
I tried this:
static PropertySet^ CreateSurface(SwapChainPanel^ panel, float scale)
{
PropertySet^ surfaceCreationProperties = ref new PropertySet();
surfaceCreationProperties->Insert(L"EGLNativeWindowTypeProperty", panel);
Object^ scaleVal = PropertyValue::CreateSingle(scale);
surfaceCreationProperties->Insert(L"EGLRenderResolutionScaleProperty", scaleVal);
return surfaceCreationProperties;
}
This method created a PropertySet
in C++ and then returns that to C#. This works fine and my ANGLE session is all good.
Now, if I change the code to return the PropertyValue::CreateSingle(scale)
directly, this fails again. If I pass the PropertyValue.CreateSingle(scale)
from C# into this C++ method, I find that the objects are not similar.
In the debugger locals I get this for the object constructed in C#:
0x09f10ba4 <Information not available, no symbols loaded for coreclr.dll>
Platform::Object ^
If the object is constructed in C++, I get this:
0x019162e8 2.00000000
Platform::Object ^ {WinTypes.dll!Windows::Foundation::Value<Windows::Foundation::ValueScalar<float> >}
Shouldn't the object be the same, since they are the same type? What is more scary is that if I pass this value across the boundary, the type changes.
Removed edit 2 and set it as an answer.