Java2D:BufferedImage在Ubuntu上没有加速

Sco*_*ott 5 java ubuntu performance java-2d

我们目前正在使用Java2D API开发Java游戏,并且在Ubuntu环境中运行它时会遇到一些奇怪的性能问题.

我们的帧速率从Windows和Mac系统上的平均62fps下降到Ubuntu上的大约10fps.经过几个小时的调试和测试各种JVM标志后,似乎是使用位掩码的BufferedImages在Ubuntu下没有加速,因为

System.out.println(img.getCapabilities(config).isAccelerated());
Run Code Online (Sandbox Code Playgroud)

打印错误.

目前我们正在加载我们的图像

img = ImageIO.read(url);
Run Code Online (Sandbox Code Playgroud)

然后使用以下方法创建与设备兼容的BufferedImage:

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;
}
Run Code Online (Sandbox Code Playgroud)

使用Transparency.OPAQUE创建兼容的BufferedImages时,标记上面的第一行代码打印为true,这表示图像现在已加速,帧速率似乎恢复正常.

然而,这当然不是我们想要的解决方案,因为图像在没有任何透明度的情况下被绘制,而是具有难看的黑色背景.

那么,有没有人知道这个问题的解决方案?

Gim*_*mby 1

我相信问题在于您在硬件加速环境中使用 BITMASK。

我不太清楚限制在哪里。

  • 只有 VolatileImage 吗?或者它也适用于 BITMASK BufferedImage 实例吗?
  • 它适用于 OpenGL 和 Direct3D 管道吗?(对此线程不谨慎;OpenGL 是 Linux 上唯一可用的)

无论如何,“解决方案”是仅在软件渲染环境中使用 BITMASK 图像;在硬件加速环境中,您需要使用半透明图像。除了旧的 javagaming.org 线程之外,我很难找到我的主张的有效来源,所以我唯一能说的就是尝试一下。

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