5

We are currently developing a game in Java using the Java2D API and are experiencing some strange performance issues when running it in an Ubuntu environment.

Our frame rate drops from an average of 62fps on Windows and Mac systems to about 10fps on Ubuntu. After some hours of debugging and testing various JVM flags it seems to be that BufferedImages using a bitmask are not being accelerated under Ubuntu because

System.out.println(img.getCapabilities(config).isAccelerated());

prints out false.

Currently we are loading our images via

img = ImageIO.read(url);

and are then creating a device compatible BufferedImage using the following method:

private static BufferedImage createCompatibleImage(BufferedImage img) {

    // Get default graphics device
    GraphicsDeviceService graphicsDevice = ServiceProvider
            .getService(GraphicsDeviceService.class);
    GraphicsConfiguration config = graphicsDevice
            .getGraphicsConfiguration();

    // Get desired transparency mode
    int transparency = img.getColorModel().hasAlpha() ? Transparency.BITMASK
            : Transparency.OPAQUE;

    // Create device compatible buffered image
    BufferedImage ret = config.createCompatibleImage(img.getWidth(),
            img.getHeight(), transparency);

    // Draw old image onto new compatible image
    Graphics2D graphics = ret.createGraphics();
    graphics.drawImage(img, 0, 0, null);
    graphics.dispose();

    // Return compatible image
    return ret;
}

When creating compatible BufferedImages using the Transparency.OPAQUE, flag the first line of code above prints out true, which indicates that the image is now accelerated and the frame rate seems to be back at normal.

However this is of course not our desired solution since the images get drawn without any transparency at all and instead have ugly black backgrounds.

So, does anyone know a solution to this problem?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Scott
  • 51
  • 1
  • Are you using a video driver in Ubuntu that supports hardware-accelerated graphics? – Jesper Jun 09 '11 at 09:30
  • Yes, I'm using a video driver that supports hardware acceleration and as stated above the game runs fine if there is no transparency involved. – Scott Jun 09 '11 at 10:23
  • I suggest stepping into the isAccelerated() code and finding out why it's returning false. – Gili Jun 11 '11 at 16:31
  • Tried to use VolatileImage instead? – Paŭlo Ebermann Jun 20 '11 at 00:51
  • 1
    Another question: If you are making a game, why are you using BufferedImage/Canvas at all? It would be much, much faster to just use JOGL for all your rendering needs. – Mikola Jun 20 '11 at 19:06
  • 1
    @Mikola: JOGL adds a native library to the dependencies which can be a major problem (for example when running as an Applet) and complicates deployment. – Joachim Sauer Jun 21 '11 at 13:27

1 Answers1

1

I believe the trouble is in the fact that you use BITMASK in a hardware accelerated environment.

I am not exactly clear about where the limitations are.

  • only VolatileImage? Or does it also apply to BITMASK BufferedImage instances?
  • does it apply to both the OpenGL and Direct3D pipelines? (not prudent to this thread; OpenGL is the only one available on Linux)

In any case the "solution" is to use BITMASK images only in software rendered environments; in hardware accelerated environments you need to use TRANSLUCENT images in stead. Its hard for me to find a valid source for my claim other than an older javagaming.org thread, so the only thing I can say is to try it out.

http://www.java-gaming.org/index.php?topic=19561.5

Gimby
  • 5,095
  • 2
  • 35
  • 47