问题
I've been testing my app in Java 7 on Mac OS X. It's running noticeable sluggish. I used VisualVM to try and track down where the bottleneck was and found linear interpolation to be the culprit:
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
To some degree this makes sense of course. Using interpolation will slow things down. But I do not see this kind of dramatic difference between using and not using interpolation in Java 6 on OS X. In Java 6, the difference was almost negligible. (The images below represent the VisualVM profile of paintComponent()
after running through a standard animation in my app.)
With interpolation:
Without interpolation:
But in Java 7 on OS X, the difference is much more noticeable:
With interpolation:
Without interpolation:
I'm guessing the issue lies in hardware acceleration and the transition from Apple to Oracle. Perhaps Apple's Java 6 was using hardware acceleration to do the interpolation and now Oracle's Java 7 is not. Does this explain it? Is there a solution? I've tried sun.java2d.opengl=true
.
Update: I found that the issue only appears when using setRenderingHints()
to apply interpolation. If you use another method to interpolate the image, such as AffineTransformOp
, then the performance drop disappears. For example:
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
AffineTransformOp scaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
scaleOp.filter(screenSliceFiltered, screenSliceFilteredScaled);
回答1:
I think you've hit the nail on the head. In all likelihood the Apple provided JVM leveraged hardware acceleration. You might ask over on the Porters group mailing list of the Mac Port sub-project of OpenJDK.
来源:https://stackoverflow.com/questions/20108866/slow-java2d-bilinear-interpolation-in-java-7-on-mac-os-x