I am trying to scale up a backbuffer using Java2D with some sort of decent anti-aliased interpolation (such as bilinear) at runtime. The idea is that I render the scene to this image, and then scale up the image in fullscreen mode to match whatever resolution the user has.
Note that fullscreen mode is important. This does not occur in windowed mode.
Is there a fast way of doing this using hardware scaling? Javadocs suggest that it exists (-Dsun.java2d.ddscale=true) but it is having no effect for me.
Here is the code:
// Initialization in AWT Canvas
buffer_ = createImage(1280, 800);
// Several hundred large draw calls into the buffer that renders the entire scene - executes fast (~10ms)
drawScene(buffer_.getGraphics());
// Upscaling buffer to screen. If I don't upscale it executes very fast (<1ms)
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(buffer_, 0, 0, 1920, 1200, null);
And the results:
- Nearest Neighbour (about 2ms)
- Bilinear (about 40ms)
- Bicubic (about 140ms)
The image is a TYPE_INT_RGB opaque BufferedImage and I am drawing onto an AWT Canvas.
Other things I have tried:
- VolatileImage (no performance change)
- Dsun.java2d.ddscale=true (no change)
- Dsun.java2d.opengl=true (about 3x slower on all commands)
- AffineTransform (no change)
- createBufferStrategy and "pre-scaling" by calling Graphics2D.scale (much slower, scales every draw call instead of 1 large buffer). Note that the BufferStrategy without scaling offers no speed improvement anyways.
- JPanel instead of Canvas (about 1.5x slower)
- imgScalr "library" (this just calls
g.drawImage
exactly as I have it above)
Some other useful notes:
buffer_.getCapabilities(gc).isAccelerated()
returnsfalse
(in windowed mode it istrue
)- This is legacy code, I have newer code that is all GL which does the scaling very quick, but don't want to have to rewrite the legacy program. Suggests that my hardware does in fact support HW scaling of images.
- AMD HD5770 graphics card
- Windows 7 latest Java 8 running as JVM.
Thanks for any help. At this point, I am sadly considering converting everything over to GL just for this one thing... there must be an answer though!