Enable WebGL for Eclipse SWT

2019-08-12 08:28发布

I found an interesting component which is Eclipse SWT Browser. It is a binding to Webkit but didn't know how to enable WebGL in it.

One easy way is using SWT.MOZILLA, but on Linux there is a compatibility problem between GTK3 and XULRUNNER.

So I was wondering if I can set the browser mode to SWT.WEBKIT (which is done) and then access somehow to webkit settings page and set the property "enable-webgl" to true (but I didn't know how).

Thanks.

3条回答
Lonely孤独者°
2楼-- · 2019-08-12 09:03

This is fixed in latest build of SWT and will be available with 4.7/Oxygen M5. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=510082

查看更多
闹够了就滚
3楼-- · 2019-08-12 09:09

Have your system render the following HTML ... a simple test which will attempt to obtain a WebGL context ... the make or break test whether a brower handles WebGL ... you will see a popup saying WebGL either works or fails :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>WebGL - 2D Rectangle</title>
  <script>
  window.onload = main;
  function main() {

    var gl = null;
    var canvas = null;
    try {   // get WebGL context

      var canvas = document.getElementById("canvas_space");
      gl = canvas.getContext("experimental-webgl", { alpha: false });
      gl.viewportWidth = canvas.width;
      gl.viewportHeight = canvas.height;

    } catch (error_msg_gl) {

      console.log("ERROR ");
      console.log(error_msg_gl);
    }

    if (gl) {
      alert("cool your browser is webgl ready !!!");
    } else {
      alert("Fail - could NOT initialize Webgl");
    }
  }
</script>
</head>
<body>
  <canvas id="canvas_space" width="400" height="300"></canvas>
</body>
</html>

I think this might help :

WebPreferences *p = [webView preferences];
if ([p respondsToSelector:@selector(setWebGLEnabled:)]) {
    [p setWebGLEnabled:YES];
}            

[details here] (Enable WebGL in the Mac OS application on WebKit)

查看更多
你好瞎i
4楼-- · 2019-08-12 09:23

I had the same problem and the following worked for me. The issue is that the SWT does not enable webgl in webkit. To make this happen you need to change some code in SWT and rebuild.

Create build environment. I work on a Mac so I used VirtualBox to create a Ubuntu virtual machine (or use a linux machine). Make sure you install a JAVA JDK as this is required to compile the native interface code. You will also need eclipse. I then followed these instructions to setup the SWT build environment (https://www.eclipse.org/swt/faq.php#howbuildjar). I imported the org.eclipse.swt and the org.eclipse.swt.gtk.linux projects into eclipse. I was building for 32bit linux with GTK (select the appropriate build for your needs as per SWT instructions). Then build by right-click on build.xml in the org.eclipse.swt.gtk.linux project and select "Run as"-->"Ant build...". This will popup a dialog and you select the "build.jars" target and click the RUN button.

Assuming all goes well you should get output like this.

Buildfile: /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/build.xml
properties:
init:
clean:
   [delete] Deleting directory /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/@dot
   [delete] Deleting directory /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/temp.folder
properties:
init:
build.jars:
properties:
init:
@dot:
    [mkdir] Created dir: /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/temp.folder/@dot.bin
copy.gtk.src:
     [copy] Copying 692 files to /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/temp.folder/@dot.src
copy.translationfiles:
is64:
     [echo] Is64=${is64}
replace64:
     [echo] Converting java files to 32 bit
    [javac] /development/swt/eclipse.platform.swt/bundles/org.eclipse.swt/buildFragment.xml:70: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 582 source files to /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/temp.folder/@dot.bin
     [copy] Copying 6 files to /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/temp.folder/@dot.bin
    [mkdir] Created dir: /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/@dot
     [copy] Copying 848 files to /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/@dot
      [jar] Building jar: /development/swt/eclipse.platform.swt.binaries/bundles/org.eclipse.swt.gtk.linux.x86/swt.jar
BUILD SUCCESSFUL
Total time: 59 seconds

The line above the BUILD SUCCESSFUL gives you the location of the swt.jar. This build will also create the libswt*.so files if you need to install them on the target system.

Modify code. Now that we can build a swt.jar we need to modify the code. I have modified 2 java files to enable the webgl in webkit. The first file is org.eclipse.swt.internal.webkit.WebKitGTK.java. Look for the following block of code and add a definition for enable_webgl as shown.

/** Properties */
public static final byte[] default_encoding = ascii ("default-encoding"); // $NON-NLS-1$
public static final byte[] default_charset = ascii ("default-charset"); // $NON-NLS-1$
public static final byte[] enable_scripts = ascii ("enable-scripts"); // $NON-NLS-1$
public static final byte[] enable_plugins = ascii("enable-plugins");
public static final byte[] enable_universal_access_from_file_uris = ascii ("enable-universal-access-from-file-uris"); // $NON-NLS-1$
public static final byte[] height = ascii ("height"); // $NON-NLS-1$
public static final byte[] javascript_can_open_windows_automatically = ascii ("javascript-can-open-windows-automatically"); // $NON-NLS-1$
public static final byte[] locationbar_visible = ascii ("locationbar-visible"); // $NON-NLS-1$
public static final byte[] menubar_visible = ascii ("menubar-visible"); // $NON-NLS-1$
public static final byte[] SOUP_SESSION_PROXY_URI = ascii ("proxy-uri"); // $NON-NLS-1$
public static final byte[] statusbar_visible = ascii ("statusbar-visible"); // $NON-NLS-1$
public static final byte[] toolbar_visible = ascii ("toolbar-visible"); // $NON-NLS-1$
public static final byte[] user_agent = ascii ("user-agent"); // $NON-NLS-1$
public static final byte[] width = ascii ("width"); // $NON-NLS-1$
public static final byte[] x = ascii ("x"); // $NON-NLS-1$
public static final byte[] y = ascii ("y"); // $NON-NLS-1$
public static final byte[] enable_webgl = ascii ("enable-webgl"); // $NON-NLS-1$

The next file is org.eclipse.swt.browser.WebKit.java. Look for the create method and the code that changes setting in the web view. I have added a call to enable the webgl as shown.

long /*int*/ settings = WebKitGTK.webkit_web_view_get_settings (webView);
OS.g_object_set (settings, WebKitGTK.javascript_can_open_windows_automatically, 1, 0);
OS.g_object_set (settings, WebKitGTK.enable_webgl, 1, 0);

Building again. Once you have made these changes and saved them you can rebuild. Simply run the "Build.jars" target again to create a new swt.jar.

Testing. I created a sample swt browser using code from the SWT examples. Then deployed my new swt.jar and the libswt*.so to the target machine. As a test I just tried some of the many samples on the web. Lastly you may need to set the following environment variables to enable GTK3 and WEBKIT2.

SWT_GTK3=1
SWT_WEBKIT2=1

I have tested this on nvidia and intel based PC running gentoo.

查看更多
登录 后发表回答