Depressingly enough, I’m right - you have to copy it to a VolatileImage, before performing the scale - otherwise the scale won’t be hardware accelerated.
Also, rather depressingly - it appears drawImage(img, x,y,width,height,imgObs) isn’t accelerated by ddscale.
You have to use an AffineTransform.
Heres the code, minor adapation from above.
I get a solid 1050fps when windowed, 950 when the window is maximised.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.VolatileImage;
import javax.swing.JFrame;
public class Test
{
public static void main(String[] args)
throws Exception
{
System.setProperty("sun.java2d.ddscale","true");
//System.setProperty("sun.java2d.opengl","true");
// opengl pipeline doesn't work properly for me at all,
// renders vsync'ed in a window, doesn't resize the BufferStrategy when the canvas is rescaled,
// and causes a VM crash when I close the application down :S
/* Window */
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rectangle bounds = new Rectangle(10, 10, 0x200, 0x200);
window.setBounds(bounds);
window.setVisible(true);
/* Canvas, strategy */
Canvas canvas = new Canvas();
window.getContentPane().add(canvas);
window.invalidate();
window.validate();
canvas.createBufferStrategy(2);
BufferStrategy bufferStrategy = canvas.getBufferStrategy();
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
/* Image, buffer */
VolatileImage vi = gc.createCompatibleVolatileImage(0x100,0x100);
BufferedImage image = gc.createCompatibleImage(0x100, 0x100);
int[] buffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
int color = 0;
long time = 0;
long delta = 0;
int counter = 0;
long fps = 0;
while (true)
{
/* Pixels */
for (int i = 0; i < buffer.length; i++, color++)
{
buffer[i] = color | 0xff000000;
}
/* Blit to screen */
Graphics2D g = (Graphics2D)bufferStrategy.getDrawGraphics();
vi.getGraphics().drawImage(image,0,0,null);
AffineTransform at = g.getTransform();
g.setTransform(AffineTransform.getScaleInstance(canvas.getWidth()/(double)vi.getWidth(), canvas.getHeight()/(double)vi.getHeight()));
g.drawImage(vi, 0, 0, null);
g.setTransform(at);
g.setColor(Color.white);
g.drawString(String.valueOf((int)fps), 10, 10);
g.dispose();
bufferStrategy.show();
delta += System.currentTimeMillis() - time;
time = System.currentTimeMillis();
counter++;
if(counter >= 100)
{
fps = 100000/delta;
delta = 0;
counter = 0;
}
}
}
}