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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…