New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose high-level APIs to create CanvasSwapChain-s for HWNDs #915
Comments
Can't you retrieve the window DPI with GetDpiForWindow? |
Right, we could use that to automatically get the initial DPIs, and remove the Should we just not worry about this scenario? Users are still able to override the DPI value by calling the Maybe that's sufficient? 🤔 |
You could subclass the window to capture the WM_DPICHANGE message. Am I understanding correctly that Win2D tracks its own DPI values? |
I guess it does when you use higher level objects that handle that for you (eg. |
Notes from API reviewLooks good as proposed. API breakdown (click to expand):namespace ABI {
namespace Microsoft {
namespace Graphics {
namespace Canvas {
class __declspec(uuid("040AB731-08F1-469F-9BF9-5B1160F27224"))
ICanvasSwapChainFactoryNative : public IUnknown
{
public:
IFACEMETHOD(CreateForHwnd)(
ICanvasResourceCreator* resourceCreator,
HWND hwnd,
uint32_t width,
uint32_t height,
float dpi,
DirectXPixelFormat format,
int32_t bufferCount,
ICanvasSwapChain** canvasSwapChain) = 0;
};
} } } } namespace Microsoft.Graphics.Canvas;
public sealed class CanvasSwapChain
{
public static CanvasSwapChain CreateForHwnd(
IntPtr hwnd,
uint width,
uint height);
public static CanvasSwapChain CreateForHwnd(
IntPtr hwnd,
uint width,
uint height,
DirectXPixelFormat format,
int bufferCount);
} The C# APIs added to |
Overview
As part of an effort to make creating standalone Win2D-powered Win32 apps simpler, we should expose some high-level APIs to easily create a
CanvasSwapChain
object for HWND. This would allow creating a standalone WASDK Win32 app using Win2D to draw to a top level HWND swap chain very easy, without the need for any other dependency or complicated setup.It's already possible to achieve this (eg. see https://github.com/Sergio0694/Win2DSample), but this requires a good amount of non trivial setup, doing interop to create all the necessary stuff. In particular, it also requires developers to get the activation factory for
CanvasDevice
to create theCanvasSwapChain
wrapper, which especially in .NET isn't really that nice or intuitive.API proposal
WinRT doesn't allow having size-dependent parameters (ie.
HWND
), so the proposal is split into two parts:The new
ICanvasSwapChainFactoryNative
interface will be implemented by theCanvasSwapChain
activation factory:The .NET projection assembly will then manually implement these two additional APIs on
CanvasSwapChain
:This makes things very easy to use both for C++ devs (which will have C++/WinRT helpers), as well as for C# developers using the projection assembly. Specifically, in this case, the fact these two APIs are not really WinRT APIs is completely transparent.
Example use
Open questions
Should we handle DPI changes in some way after a swap chain has been created? Also see conversation below.
The text was updated successfully, but these errors were encountered: