我们目前正在使用 Java2D API 开发一款 Java 游戏,在 Ubuntu 环境中运行时遇到了一些奇怪的性能问题。
我们的帧速率从 Windows 和 Mac 系统上的平均 62fps 下降到 Ubuntu 上的大约 10fps。经过几个小时的调试和测试各种 JVM 标志后,使用位掩码的 BufferedImages 似乎没有在 Ubuntu 下加速,因为
System.out.println(img.getCapabilities(config).isAccelerated());
打印出错误。
目前我们正在通过
加载我们的图片img = ImageIO.read(url);
然后使用以下方法创 build 备兼容的 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;
}
当使用 Transparency.OPAQUE 创建兼容的 BufferedImages 时,标记上面第一行代码打印出 true,这表明图像现在被加速并且帧速率似乎恢复正常。
然而,这当然不是我们想要的解决方案,因为绘制的图像完全没有任何透明度,而是具有难看的黑色背景。
那么,有谁知道这个问题的解决方案吗?
最佳答案
我认为问题在于您在硬件加速环境中使用 BITMASK。
我不太清楚限制在哪里。
- 只有 VolatileImage?或者它是否也适用于 BITMASK BufferedImage 实例?
- 它是否适用于 OpenGL 和 Direct3D 管道? (对这个线程不谨慎;OpenGL 是 Linux 上唯一可用的)
无论如何,“解决方案”是仅在软件渲染环境中使用 BITMASK 图像;在硬件加速环境中,您需要改用 TRANSLUCENT 图像。除了较旧的 javagaming.org 线程之外,我很难为我的声明找到有效的来源,所以我唯一能说的就是尝试一下。
关于Java2D : BufferedImage not accelerated on Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6290576/