microsoft / Win2D

Win2D is an easy-to-use Windows Runtime API for immediate mode 2D graphics rendering with GPU acceleration. It is available to C#, C++ and VB developers writing apps for the Windows Universal Platform (UWP). It utilizes the power of Direct2D, and integrates seamlessly with XAML and CoreWindow.
http://microsoft.github.io/Win2D
Other
1.82k stars 288 forks source link

C++ and C# interoperability? #769

Closed FrayxRulez closed 4 years ago

FrayxRulez commented 4 years ago

Hello, I'm pretty sure this is a noob question, but I can't figure out myself and I can't find any example or documentation about this on the web. I have a Windows Runtime Component I use to do some images encoding/decoding and I have a method that should output a CanvasBitmap to be rendered by a AnimatedCanvasControl in the C# part of the app. At the moment I'm using CanvasBitmap::CreateFromBytes but I think it really makes a little sense to do this, as I need to create a Array<uint8> when I have BYTE* to be consumed, still I can't figure out how to use ICanvasBitmapFactory (that seems to accept a pointer) and I'm not even sure if after I could pass the result back to C# as CanvasBitmap.

shawnhar commented 4 years ago

There are multiple different ways to use a WinRT component such as Win2D from C++: C++/CX, C++/WinRT, or WRL. It sounds like you are using C++/CX? The ICanvasBitmapFactory interface is part of the lower level WRL way of accessing these APIs, so not relevant if you are working with C++/CX.

The Array<> class is documented here: https://docs.microsoft.com/en-us/cpp/cppcx/platform-array-class?view=vs-2019

FrayxRulez commented 4 years ago

I’m using C++/CX. I would like to avoid using Array<> (that I’m using already) as the method is being invoked 30/60 times per second and I would like to further optimize it.

FrayxRulez commented 4 years ago

So that's it: https://github.com/FrayxRulez/RLottie.UWP/commit/fb3766ea8c90d969319164bde3fc860a080d0210

Thanks anyway!

shawnhar commented 4 years ago

There is no need to drop down to the ABI level here. You can get identical performance (same number of allocations, same number of data copies, etc.) using C++/CX and the Array<> type.