Skip to content
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

Question: How to use CompositionPathGeometry and CompositionPath? #3609

Closed
zed220 opened this issue Nov 14, 2020 · 12 comments
Closed

Question: How to use CompositionPathGeometry and CompositionPath? #3609

zed220 opened this issue Nov 14, 2020 · 12 comments
Labels
product-winui3 WinUI 3 issues question team-CompInput Issue for IXP (Composition, Input) team wct

Comments

@zed220
Copy link

zed220 commented Nov 14, 2020

Hello,

I try to use CompositionPathGeometry like this:

IGeometrySource2D source = <...>
var pathGeometry = Compositor.CreatePathGeometry(new CompositionPath(source));

But I can't find how to create the source (IGeometrySource2D).

Can you create an example?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 14, 2020
@StephenLPeters
Copy link
Contributor

@jesbis @jeffstall could one of you weigh in here?

@StephenLPeters StephenLPeters added the team-CompInput Issue for IXP (Composition, Input) team label Nov 17, 2020
@jesbis
Copy link
Member

jesbis commented Nov 17, 2020

@duncanmacmichael do we have a sample for this or should we update the docs?

@zed220 I believe you can use the Win2D CanvasGeometry as a concrete implementation:

http://microsoft.github.io/Win2D/html/T_Microsoft_Graphics_Canvas_Geometry_CanvasGeometry.htm

e.g.:

var pathBuilder = new CanvasPathBuilder(new CanvasDevice());
pathBuilder...
var pathGeometry = CanvasGeometry.CreatePath(pathBuilder);
var path = new CompositionPath(pathGeometry);

@zed220
Copy link
Author

zed220 commented Nov 18, 2020

@jesbis As I can see, the CanvasPathBuilder is a part of the nuget package Win2d.uwp. In my opinion, it shouldn't be used in WinUI?

@jesbis
Copy link
Member

jesbis commented Nov 18, 2020

There are a couple issues tracking enabling WinUI support:

microsoft/Win2D/issues/707

microsoft/WindowsAppSDK/issues/213

@pratikone pratikone added product-winui2 product-winui3 WinUI 3 issues and removed needs-triage Issue needs to be triaged by the area owners product-winui2 labels Oct 14, 2022
@tom-huntington
Copy link

tom-huntington commented Jun 27, 2023

@jesbis Can Direct2D implement IGeometrySource2D? I've tried full copy-paste

ID2D1PathGeometry* m_pPathGeometry;
...
auto iGeometrySource2D = Windows::Graphics::IGeometrySource2D{ m_pPathGeometry, winrt::take_ownership_from_abi };
auto compositionPath = Microsoft::UI::Composition::CompositionPath{ iGeometrySource2D };
// 0x80070057 : 'Invalid argument to parameter source. Object parameter must not be null.'.

but maybe there is some QueryInterface trick I'm not doing right.

@ChewWorker
Copy link

I haven't had a chance to try raw D2D, but it can be done with Win2D. The following example works to use Win2D to build the geometry source. One possible issue with the code you pasted is that there aren't open/add geometry/close steps being done before creating the composition path. It's possible the d2d object defaults to a bad/closed state unless the geometry has beeen created. This code was used in a basic UWP/Win2D project:

        var c = Window.Current.Compositor;

        // CanvasGeometry and CanvasPathBuilder are Win2D objects
        CanvasPathBuilder pathBuilder = new CanvasPathBuilder(sender);
        pathBuilder.BeginFigure(1, 1);
        pathBuilder.AddLine(300, 300);
        pathBuilder.AddLine(1, 300);
        pathBuilder.EndFigure(CanvasFigureLoop.Closed);

        CanvasGeometry geometry = CanvasGeometry.CreatePath(pathBuilder);

        // These are Composition Path objects
        var pathGeometry = c.CreatePathGeometry(new CompositionPath(geometry));

        // Need to create a sprite shape from the rounded rect
        var roundedRectSpriteShape              = c.CreateSpriteShape(pathGeometry);
        roundedRectSpriteShape.FillBrush        = c.CreateColorBrush(Colors.Red);

        _shapeContainer.Shapes.Add(roundedRectSpriteShape);

@duncanmacmichael
Copy link
Member

duncanmacmichael commented Jun 27, 2023 via email

@tom-huntington
Copy link

ID2D1Geometry is COM interface (IUnknown) but not a WinRT interface (IInspectable) but

IGeometrySource2D(void* ptr, take_ownership_from_abi_t) noexcept : winrt::Windows::Foundation::IInspectable(ptr, take_ownership_from_abi) {}

expects ptr to be Inspectable. No doubt what I did shouldn't work.

I guess IGeometrySource2D is the WinRT placeholder for ID2D1Geometry and you have to do some kind of dance creating an object that inherits from both. Maybe I should just use the Win2D bloat

@castorix
Copy link

castorix commented Jun 29, 2023

With Windows.UI.Composition, it is done with IGeometrySource2DInterop, like in https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/blob/master/cpp/HelloVectors/HelloVectors_win32.cpp,
but not sure if it works with Microsoft.UI.Composition (I could only test in C# and I got 0x80070057 like @tom-huntington )

@tom-huntington
Copy link

@castorix Thanks! I managed create the Microsoft::UI::Composition::CompositionPath in c++. Hopefully there aren't any more problems. It should work since it only wants a Windows::Graphics::IGeometrySource2D.

@RoyLicx
Copy link

RoyLicx commented Nov 8, 2023

Currently, Win2d is supported in Winui3. You could check the document-Overview of Win2D - Windows apps | Microsoft Learn. You could use CompositionPathGeometry and CompositionPath just like @jesbis shows.
Please also note that Win2D for WinUI 3 (Windows App SDK) is a work-in-progress now, and some features aren't supported.

@duncanmacmichael
Copy link
Member

Closing as completed, as this appears to have been answered with no activity since June (other than Roy's comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product-winui3 WinUI 3 issues question team-CompInput Issue for IXP (Composition, Input) team wct
Projects
None yet
Development

No branches or pull requests

10 participants