Skip to content

Hosting Avalonia in WebAssembly #1387

Closed

Description

@itsamelambda

Would it be possible to host Avalonia with WebAssembly and .NET in a browser?

Then we could write the same C# code and run it native in all browser and on all desktops.

Activity

  1. kekekeks commented on Feb 24, 2018

    @kekekeks
    Member

    It would be possible and we are currently considering our options. The major blocker is that HTML5 doesn't have a proper 2D drawing API, so Skia needs to be compiled to wasm and somehow linked to resulting application. The problem with linking is that Mono doesn't currently provide P/Invoke support for wasm target. Another problem is that P/Invoke means indirect calls (i. e. calls via pointer), while WebAssembly doesn't support indirect calls between modules. That means that Skia needs to be linked statically, so we'll need a custom Mono build.

    It would be easier with CoreRT, since it supports static linking out of the box, but they are still implementing MSIL instructions for wasm target, so it's not ready even for an experimental port.

  2. legistek commented on Mar 7, 2018

    @legistek

    Sorry if this is an obviously terrible idea, but why wouldn't you use WebGL?

  3. danwalmsley commented on Mar 8, 2018

    @danwalmsley
    Contributor
  4. kekekeks commented on Mar 9, 2018

    @kekekeks
    Member

    @pmoorelegistek We will be using WebGL at some point. The problem is that WebGL by itself doesn't support drawing 2D vector graphics. So we need Skia anyway.

  5. legistek commented on Mar 9, 2018

    @legistek
  6. legistek commented on Mar 9, 2018

    @legistek

    Hmm. The problem with compiling Skia to WASM is you'll lose any benefits of hardware graphics acceleration on the device. Browser will be doing all the math, painting pixels, etc. I don't know that you'd ever get anything close to 60fps with a complex UI.

    Have you tried just using the HTML5 Canvas or SVG?

  7. kekekeks commented on Mar 9, 2018

    @kekekeks
    Member

    Skia can use OpenGL ES profile for hardware-accelerated rendering. Which is basically what is provided by WebGL.
    HTML5 Canvas and SVG are not sufficient to render Avalonia.

  8. legistek commented on Mar 9, 2018

    @legistek

    You're saying Skia / OpenGL will be able to access hardware acceleration (i.e. the GPU) after compiled to WASM? Are you certain about that? Everything I understand about WASM would lead me to think not.

    I would think you'd be better off using three.js - or better yet porting it to C# - wrapped around WebGL.

    I'd be happy to look into it some more and experiment. Do you have a working prototype using Mono-to-WASM at the moment with any rendering at all?

  9. kekekeks commented on Mar 10, 2018

    @kekekeks
    Member

    You're saying Skia / OpenGL will be able to access hardware acceleration (i.e. the GPU) after compiled to WASM

    Emscripten toolchain has complete support for WebGL. It might be a bit difficult to make it work with Mono since it has it's own js glue code, but other than that I don't see any issues.

  10. danwalmsley commented on Mar 15, 2018

    @danwalmsley
    Contributor
  11. kekekeks commented on Mar 16, 2018

    @kekekeks
    Member

    @danwalmsley it uses HTML. Avalonia can't be rendered with HTML.

  12. adoris commented on Mar 16, 2018

    @adoris

    Is there are any try to use Skia/WebGL? Any branch for this experiment?

  13. legistek commented on Mar 16, 2018

    @legistek

    if I understand right, what @kekekeks is saying is that it's not feasible at the moment because Mono can't use P/invoke to call the Skia functions. So while they can compile Skia to WASM, there would be no way for the .NET code to access it.

    What I don't understand fully, though, is why it has to be Skia. There are Javascript libraries that provide 2D drawing function wrappers over WebGL (e.g., https://threejs.org/). I'd think it at least worth an experiment to make a DrawingContext that wraps three.js and go from there. If that works, porting threejs to C# and simply including it in the Avalonia build would not be too bad of a project and would probably get you performance on par with Skia.

  14. kekekeks commented on Mar 17, 2018

    @kekekeks
    Member

    @pmoorelegistek The main problem is the complete lack of a proper text measurement API. We can't get our TextBox to work if we don't have an accurate position of each individual symbol. Even for our TextBlock we need to be able to measure and lay out text lines properly. There is no way we can do that with the APIs available to JavaScript.

  15. 56 remaining items

  16. aboimpinto commented on Sep 29, 2020

    @aboimpinto

    Uno Platform (https://platform.uno/) is rendering the WPF from Microsoft to WebAssembly. There should not that difficult to port the code to render Avalonia too ...

    Maybe, there could be a way to join efforts ...

  17. Gillibald commented on Sep 29, 2020

    @Gillibald
    Contributor

    Uno does not render anything as Avalonia does. They use HTML elements.

  18. yowl commented on Sep 29, 2020

    @yowl

    They also have a Skia backend, not sure it's generally available for Wasm yet, but it is the plan.

  19. birbilis commented on Oct 12, 2020

    @birbilis

    related comment:

    On Windows 7, we still have the WebAssembly story, but we also announced a new Skia backend, that can be wrapped in a WPF shell so you can run "natively" and outside of a browser context.

    https://developercommunity.visualstudio.com/idea/378455/net-ui-standard.html

    also:

    When using Uno Platform to get your UWP/WinUI 3.0 and later apps on Windows 7 you now have THREE options for running your app on Windows 7: 1. Run your app as a PWA. Your Windows 7 will need Chrome installed on it. 2. Run your app on Chromium through Electron. 3. Use Uno Platform 3.0 supported Skia backend, that can be wrapped in a WPF shell so you can run natively on Windows 7 and outside of a browser context.

    https://platform.uno/windows7/

  20. aboimpinto commented on Oct 12, 2020

    @aboimpinto

    What about Ubuntu (Linux environment)?

  21. birbilis commented on Oct 13, 2020

    @birbilis

    regarding Uno and Linux see here: https://platform.uno/uno-platform-for-linux/

  22. aboimpinto commented on Oct 13, 2020

    @aboimpinto

    regarding Uno and Linux see here: https://platform.uno/uno-platform-for-linux/

    I was referring Avalonia in Linux environment compiling into WebAssembly

  23. birbilis commented on Oct 13, 2020

    @birbilis

    guess Avalonia could also get ideas from Uno, or maybe even use it as a backend or something?

  24. Gillibald commented on Oct 13, 2020

    @Gillibald
    Contributor

    Just to be clear. Text rendering is no longer an issue. The "only" thing we need is a platform implementation for WASM that is setting up WebGL and exposes all input events. The threading implementation might also be tricky.

  25. congzhangzh commented on Jul 11, 2021

    @congzhangzh

    May be we can learn from google flutter, skia should not be a problem!

  26. starikcetin commented on Jul 28, 2021

    @starikcetin

    I don't know if it is of any help, but there is this: lume/glas: WebGL in WebAssembly with AssemblyScript

  27. mikernet commented on Oct 18, 2021

    @mikernet
  28. locked and limited conversation to collaborators on Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions