I have a java application I wrote that loads up a TTF font and uses the drawString
method from Graphics2D. This gets called every 50ms with the x and y positions changing each time to make the text move. When I run the program on Windows, I get 0-1% CPU usage, but on Mac开发者_Go百科 I get about 75% usage. This Windows machine does have a better CPU but there's no way there should be that big of a difference. I think it has to do with Hardware Acceleration and I want to know how enable it. I found some Mac specific Java properties, but none of them lowered my CPU usage. Any ideas how to increase Java 2D performance on OS X? Thanks.
EDIT1: I thought that these properties would help but they didn't.
System.setProperty("sun.java2d.opengl", "true");
System.setProperty("apple.awt.graphics.UseQuartz","true");
System.setProperty("apple.awt.graphics.EnableQ2DX","true");
EDIT2: You can download the project source and byte code here: http://drop.io/ExampleScreenSaver
EDIT3: Since drop.io no longer exists, I uploaded the project to Google Code. Now you can browse the code without downloading it. I still have no idea how to approach this problem so any help would be appreciated.
Run the profiler in "jvisualvm" to identify where the time goes.
Apple JVM font handling is specific to MacOS X. For instance, if a glyph is not found for a character in the current font, the JVM looks for another font to display the character. Also, antialiasing is enabled by default (you should check that you have it enabled on Windows if you want to compare). I am not surprised Apple's implementation is using more CPU.
OpenGL can be enabled when calling java e.g.
java -Dsun.java2d.opengl=true MyJavaGame
Too enable opengl from inside your java application
System.setProperty("sun.java2d.opengl", "true");
Not sure how this pans out on the iOS / Linux / Android and may require more looking into.
精彩评论