Implementation of a problem with copying a buffer with a copy of Java

he is ppl!

im writing a game. since not much has changed over time, I switched to buffering parts of the board (on which the game was played) and copied it from time to time. I am changing the content in the foreground, so I still need high fps. I also want to zoom in, and here the fun begins: to save memory, I reuse buffers. Whenever I increase application latency, it runs fine.

After profiling, I came with two manufacturers:

  • cleaning the backbuffer (4000x4000 pixels, takes about 29 ms. To maintain transparency, I use g.fillRect)

  • Copying the buffer back to the real image (which, of course, is not live, but again from getBufferStrategy ()). It takes 300 ms, next time about 150, then from the third frame it works smoothly.

To find out where the problem may be a bit of code. I create my own buffers as follows:

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = env.getDefaultScreenDevice(); GraphicsConfiguration config = device.getDefaultConfiguration(); image = config.createCompatibleImage(width, height,Transparency.TRANSLUCENT); 

And now the part where I copy the buffer back to the image. Please note that I need to cut out some parts of the buffer, so I went to the call with the maximum parameter.

 g.drawImage(image, vs.boardOffsetX, vs.boardOffsetY, targetWidth, targetHeight, 0, 0, sourceWidth, sourceHeight, null); 

Finally, for my other problem: I clean the images as follows:

 Graphics2D g = (Graphics2D) image.getGraphics(); Color transparent = new Color(0, 0, 0, 0); g.setColor(transparent); g.setComposite(AlphaComposite.Src); g.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); 

Thank you very much! stuck on this for quite some time. And feel free to give me stylistic advice on my approach: his first honest attempt to make graphics.

Thanks!

edit: The part that I really don't understand is that the exact same operation takes so many times. Since only AWT-Thread works in addition to my thread, I get twice ~ 300 ms, and then it down like 10 ฮผs !!!! and it is AWFULLY fast for copying 16 million pixels. Does anyone understand this effect? and perhaps knows a way to โ€œpre-optimizeโ€ this behavior?

+4
source share
2 answers

You may be interested to hear that I cannot solve the problem. Instead, I canceled the last large-scale step, limiting the largest buffer to 2000x2000 and eliminating the worst part of the problem. Now the scaling is pretty smooth (I measured 80 ms for the first call to drawImage. It's still pretty much, but since the view is changing rapidly, you really don't notice it).

In my research, I also found that I cannot explicitly force java to create accelerated VolatileImages of the required size. This can be a problem - God knows why. I would still like to know though ...

But giving ppl with similar problems is one last word of advice: travel around the world.

thank you for your help!

+2
source

I donโ€™t know if this will help you, but my copy of the application will speed up a bit with System.arraycopy () .

 BufferedImage tmp = (BufferedImage) img; int[] src = ((DataBufferInt) tmp.getRaster().getDataBuffer()).getData(); int[] dst = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData(); System.arraycopy(src, 0, dst, 0, dst.length); 
+1
source

Source: https://habr.com/ru/post/1379039/


All Articles