I have asked this question some time ago to get an idea of speeding up image writing to file.I basically have an OpenGL app.At the end of each render loop I save the frame buffer into image file.
fbo.readFrame();
glReadPixels(0, 0, _viewportWidth, _viewportHeight, GL_RGBA, GL_UNSIGNED_BYTE, _data);
_data.rewind(); //ByteBuffer
new TImageExporter(ImageExporter.TGA, "renderings/", _data, _viewportWidth, _viewportHeight, true, _frameCount++).run();
The TImageExporter extends Thread and inside the "run()" method I execute writing to File. To my surprise The render loop for 50 frames takes almost the same time as if I use a single thread version of the Image Exporter. (3293 milliseconds -multi-threaded and 3437 milliseconds using single threaded).What do I do wrong here? That is the code inside TImageExporter:
public void export() {
_pixels = new int[_width * _height];
int bindex;
int plenght = _pixels.length;
// convert RGB data in ByteBuffer to integer array
for (int i = 0; i < plenght; ++i) {
bindex = i * 4; //use 3 for RGB
//// here write the pixels to RGBA/////
............
.......................
}
_image.flush();
_image.setRGB(0, 0, _width, _height, _pixels, 0, _width);
_image = ImageUtils.flipY(_image);
renderByFormatType();
}
private void renderByFormatType() {
String formattedString = String.format(_formatString, _frameCount);
if (_exportType.equals(TGA)) {
try {
writeTGA(_image, new File(_renderPath + "screenshot_test" + formattedString + ".tga"), _flipX);
} catch (IOException ex) {
Logger.getLogger(TImageExporter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void run() {
export();
}
UPDATE: People have asked here if I write to the same file.No, each thread writes to a completely new file.
UPDATE1: Set a global static variable that holds array of BufferedImage.Now each new TImageExporter writes the image data to that array to a different index.All I got is 3038 milliseconds instead of 3437 when writing directly to Disk.