Skip to content

Allow embedders to draw custom elements #3646

Open

Description

@kmcallister

I've heard a lot of interest in using Servo as the UI toolkit for native apps in Rust. This basically turns the notion of an embeddable browser inside-out.

Imagine a 3D modelling program. The program's window is simply a chrome-less instance of Servo. The UI is designed in HTML + CSS and its behavior is specified in JS. The computational and graphical heavy lifting is done by C++ or Rust code that draws to a <canvas> element using native OpenGL calls. Like every other part of the UI, it can be styled and transformed by developers with no knowledge of OpenGL or C++/Rust.

This has some advantages over Emscripten + WebGL:

  • True native-code performance where it matters
  • Real native threads of any form you like. You can even use realtime scheduler modes which (afaik) would be very hard to do in Emscripten.
  • Access to hardware and native APIs that don't have equivalents on the Web. Mozilla generally believes in closing this gap instead, but it's a never-ending task and there's some stuff that won't ever be in the Web platform. (I'm thinking of my laser cutter that was controlled from Linux-CNC by bit-banging stepper motor waveforms out the parallel port.)
  • Lower memory footprint, using a stripped down copy of the Servo engine rather than a browser with full web compatibility
  • Access to whatever OpenGL calls are supported by your drivers, instead of the subset provided by WebGL

Some apps can take advantage of these features but will also work in Emscripten + WebGL with reduced performance and functionality. In those cases, you can have a single codebase that builds as either a native app or an ordinary web page.

Once we have basic support for WebGL <canvas>, I think we could implement this variant with relative ease. We'll probably want to use the texture-sending machinery so that embedders can use their own OpenGL context.

Activity

  1. added
    B-interesting-project
    on Oct 11, 2014
  2. changed the title [-]Allow embeders to draw custom elements[/-] [+]Allow embedders to draw custom elements[/+] on Oct 11, 2014
  3. brendanzab commented on Oct 11, 2014

    @brendanzab
    SponsorContributor

    In #3579 and IRC @mbrubeck mentioned that he was working on turning the windowing library dependency 'inside out' - ie. Servo could be used with any OpenGL context. Could that work help with this?

  4. pcwalton commented on Oct 11, 2014

    @pcwalton
    Contributor

    I'm a little worried about essentially recreating NPAPI.

  5. pcwalton commented on Oct 11, 2014

    @pcwalton
    Contributor

    That said if it's not exposed to content it seems less bad.

  6. kmcallister commented on Oct 11, 2014

    @kmcallister
    ContributorAuthor

    What do you think are the main lessons learned from NPAPI?

  7. pcwalton commented on Oct 11, 2014

    @pcwalton
    Contributor

    Someone who's actually worked with it would have to chime in, but I think the biggest issues were:

    • Exposing native code to content is a security nightmare.
    • Native code tends not to interact well with user control over the browser, because it can talk to OS APIs without the browser knowing. (The biggest symptom of this is "why can't I mute background tabs?")
    • Keeping the API stable and preventing plugins from breaking in browser upgrades is really hard.
    • Fixing the above issues required recreating the Web APIs in ad-hoc ways (e.g. Pepper), which begs the question of why not just use the Web APIs in the first place.

    Of course there may well be ways to mitigate these issues, I'm just saying what I understand the problems with NPAPI to be.

  8. mcpherrinm commented on Oct 11, 2014

    @mcpherrinm

    Exposing native code to the web is surely a bad idea. I didn't think that's what kmc was proposing (or at least that's not where I see value).

    I loathe making simple UIs to wrap around the OpenGL hacks I've written, and usually end up making something crummy in OpenGL. I'd much rather write native code, whether Rust or C++, with the native OpenGL features available, and all the host APIs that aren't suitable for exposing to JS.

  9. Ms2ger commented on Oct 11, 2014

    @Ms2ger
    Contributor

    I'm somewhat skeptical about putting our limited resources in non-web stuff at this point.

  10. EdorianDark commented on Oct 11, 2014

    @EdorianDark
    Sponsor

    It would be for most applications enough to call Javascript functions and to expose functions which would be callable by Javascript.

  11. kmcallister commented on Oct 11, 2014

    @kmcallister
    ContributorAuthor

    Exposing native code to the web is surely a bad idea.

    Well, it's a bad idea that people keep attempting in various forms ;)

    But yeah, the conservative version of this isn't really "the web" at all. The embedded copy of Servo wouldn't support HTTP or even file:// URLs; it gets all its resources directly from the embedding app.

    Of course that does negate some of the value of using Web tech as your UI toolkit, and in practice I think a lot of people would want to hook up HTTP content, which will be a can of worms.

    I'm somewhat skeptical about putting our limited resources in non-web stuff at this point.

    Fair enough. I mostly opened this ticket in hopes of drawing new contributors from the game dev community or such :)

  12. wickerwaka commented on Oct 11, 2014

    @wickerwaka

    Coherent UI (http://coherent-labs.com/overview/) is an existing example of HTML and related technologies being used for game UIs. For games, and maybe other applications, developers want a lot of control over the final presentation and wrapping the your view up inside a and rendering that would be limiting. I believe the approach Coherent takes is allowing you to create and draw multiple UI frames wherever you want into your current rendering context. This allows you to draw your 3d scene (maybe with UI within that scene itself), apply post processing, draw your UI(s) on top of that, then maybe additional post processing on that combined view.

  13. jmacdonald commented on Mar 17, 2015

    @jmacdonald

    I think this is a great idea. The main difference (in my mind) between this and NPAPI is that the point is not to extend the browser's functionality, but to use the browser's rendering engine to build a UI using HTML, CSS and JS. It effectively replaces Qt and GTK+ as a UI layer for Rust. There are already projects aimed at desktop application development using a web stack:

    1. http://nwjs.io/
    2. https://github.com/atom/atom-shell

    The main drawback to these is that they use JavaScript exclusively for all of the application code. While still very useful for handling events, it would be brilliant to be able to run computationally expensive tasks in Rust. You could build a cross-platform photo management application capable of non-destructive edits and filters. A text editor could be built to do all of its lexical analysis and data management in Rust, leaving JS to handle user events and HTML/CSS to present the UI. To that effect, the author of Sublime Text wrote a brief statement about how he builds for various platforms; it would seem like an ideal candidate for a project like this.

    I don't picture this being a part of Servo itself. It would be more about shaping its API so that such a thing would be possible, and having a separate framework project that provides the configuration to bootstrap an application with the necessary components. Something where the Rust and JavaScript interface API is clearly defined and the rendering engine has the necessary hooks to load an initial HTML file with associated (local) CSS and JS files. The aforementioned application development frameworks have done all of this (albeit with Chromium), but have chosen to extend JavaScript with NodeJS, rather than with another language. Having Rust as the extension to JavaScript would be, in my opinion, far more powerful.

  14. miketang84 commented on Jan 11, 2016

    @miketang84

    compared to QT environment.
    Rust <--> C++
    Html <--> XML
    Js <--> QML
    Servo <--> Qt

    all are wonderful. I think we should build a native system do not contain javascript, and consider html as optional.

    https://github.com/daogangtang/panda-rs

  15. bugmenot2 commented on Feb 26, 2017

    @bugmenot2

    Are we going to be able to hook into DOM without JS any time soon? Is anyone looking into this? Thanks

  16. jdm commented on Feb 26, 2017

    @jdm
    Member

    Nobody is looking into that. It poses big challenges and would require a lot of work that would not benefit Servo's primary use case at all.

  17. bugmenot2 commented on Feb 27, 2017

    @bugmenot2

    @jdm I reckon JS already maps to native methods, so why would it be hard to expose the same interface natively? Surely shipping my own browser with my app should be able to free me from the tyranny of JS

  18. BrenBarn commented on Apr 18, 2017

    @BrenBarn

    Strongly agree with bugmenot2. The inability to separate the HTML/CSS rendering engine from the JS engine is to me the critical limitation of the "web platform". This is also in line with modularity, which is one of the explicit goals stated right on the front page of servo.org . The rendering engine and the JS engine are two very different and very separate things, and are a natural thing to modularize.

  19. nox commented on Sep 30, 2017

    @nox
    Contributor

    The whole DOM is owned by the JavaScript GC. The JavaScript engine is at the very core of Servo.

  20. BrenBarn commented on Sep 30, 2017

    @BrenBarn

    Yes. That is the problem. Why is the core not just a DOM rendering engine, with the language for manipulating it as a fully pluggable component? What is modular about Servo?

  21. nox commented on Sep 30, 2017

    @nox
    Contributor
  22. BrenBarn commented on Oct 15, 2017

    @BrenBarn

    I see. So instead we just recreate the status quo, which is that it's impossible to render HTML in anything that is not a browser.

    I had hoped that Servo's goal of modularity would finally open the door to having usable HTML rendering in things that are not browsers, but I guess my hopes are dashed.

  23. nox commented on Oct 15, 2017

    @nox
    Contributor

    As mentioned by @jdm, this is simply not a priority. Let's not make it sound like it would be an easy task to make Servo's core "just a DOM rendering engine", to reuse your own words. Things are tightly tied together by sheer necessity, nothing else.

  24. SimonSapin commented on Oct 15, 2017

    @SimonSapin
    Member

    PrinceXML is not a browser. It’s a batch processing thing (as in: non-interactive) that transforms HTML/CSS documents into PDF. And yet, even they ended up adding JavaScript support because enough of their customers needed it.

    The time of JavaScript being an optional part of the web platform is well past.

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

    A-embeddingB-interesting-project

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions