OpenJFX/JavaFX WebView not working with WebGL

Hello

I tried to open a simple demo based on WebGL in a WebView, please find my source code below:

package io.sourceforge.tuer;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();

        webView.getEngine().load("https://webglsamples.org/blob/blob.html");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

However, I just get the following message instead of seeing the blobs moving:

This page requires a browser that supports WebGL.
Click here to upgrade your browser.

It’s strange as a member of Gluon support team told me that WebGL is supported by JavaFX/OpenJFX.

I just tried this, and am getting the same result. I’m using Maven to build, and tried using the latest version from org.openjfx (18-ea+1) and it still doesn’t work, so adding WebGL must not be a recent development.

I AM able to get other websites to display just fine, with the same code, so it’s clearly something to do with the browser code called forth by JavaFX not supporting WebGL.

This is sad. I am seeing there’s be a request for this capability for at least half a dozen years or more.

IDK if this thread from Chromium Embedded Framework offers any hope of being able to display a WebGL site via Java.

Thank you so much for your help. I’ve found the source code involved in the use of WebGL in OpenJFX by Gluon. Actually, it uses Bck2Brwsr to run Java programs using OpenJFX directly in a web browser without any Java plugin by transpiling Java to Javascript and it translates OpenGL into WebGL. However, it doesn’t allow to support WebGL in WebView.

NativeFX isn’t actively maintained :frowning: FxJCEF is known to be slow (it doesn’t use the very latest OpenJFX integration in JOGL available since the version 2.4.0) :frowning: JCEF itself is very unstable, I saw someone trying to use it for months, it was sometimes crashing under Windows :frowning: jfx-cef only supports Windows :frowning: https://gitlab.com/linasch/jfx-cef/-/commit/2d11e5ccf968158ac2bef6ba82a11bc032b933a5#ec4ac5d6aeca2f1dbfc20dba6468968f5d94ea59_0_61 MiniBlink works only under Windows too :frowning:

I think that a more viable solution to use WebGL and WebGPU from Java consists in using Dukescript as a source of inspiration as it already supports the Canvas API to implement WebGL and WebGPU support in Bck2Brwsr. Please note that WebGL seems to be already supported somehow in GWT and TeaVM. @SHC maintains WebGL4J, it’s a viable option too.

I tried maven-typings-plugin with a Typescript definition of WebGL 1, it didn’t work but I’m still investigating. If it ends up by working, we’ll be able to use the generated Java source code in projects compatible with Apache HTML/Java API. Otherwise, I’ll have to write the necessary source code by myself.

1 Like

I’m not so sure if this workaround works, but it should work in theory. Con is, it involves a lot of work and I don’t think it is worth it.

The trick lies in creating a bridge between JavaFX and the JavaScript running in the WebView with this snippet.

public class JSBridge {
    public void helloWorld() {
        System.out.println("Called from JavaScript: Hello World!");
    }
}

// In WebView creation code
JSBridge bridge = new JSBridge();

WebEngine engine = webView.getEngine();
JSObject window = (JSObject) engine.executeScript("window");

window.setMember("JSBridge", bridge);

engine.load("http://localhost:8080/");

And the JS page can have a script which does

JSBridge.helloWorld();

The code from our bridge function will get executed.

Now, how is this going to help get WebGL?

We can fake an API here, like make a polyfill object on Canvas element, and let it redirect all calls on it to the native. The native will redirect them to actual OpenGL calls.

This is tricky, definitely a lot of work involved, but should work in theory. Not really sure why we need WebGL in JavaFX though, when we can do that from native itself.

If Webkit is compiled without WebGL support, it won’t work.

I meant faking WebGL with a real OpenGL using bridges. Basically you’re writing a translator with this approach, and it doesn’t matter if Webkit is compiled with WebGL support or not.

I posted a question on the openjfx-dev mailing list. (A bit of formatting for clarity, not an exact quote.)

Kevin Rushforth kevin.rushforth at oracle.com
Thu Aug 26 18:55:06 UTC 2021

It would be a lot of work, and not something we are likely to do any time soon. – Kevin

On 8/26/2021 10:46 AM, Phil Freihofner wrote: > All this recent, great activity on WebView makes me wonder: are there plans in place, or any teams actively working on enabling WebGL? I was recently shown, and tried myself to display a site at webglsamples with no success. What are the hurdles? Are they mostly technical, or is it mostly a matter of getting the proper permissions and authorizations?

1 Like

I saw his reply. I thought it only required to update Webkit but maybe I underestimated the necessary effort to make it happen.

1 Like

Yes it might work but I might need to target platforms with WebGL support but with inconsistent or half broken support of OpenGL.