BufferedImage vs. VolatileImage

I was under the impression that VolatileImages are the only images that take advantage of VRAM (and hence have much superior speed). However I’ve also heard that BufferedImages do this as well. Can anyone confirm or refute this?

I’d like to be able to use ImageIO.read() to create my images. Are all BufferedImages accelerated, or only those created using a call to the GraphicsConfiguration ie : graphicsConfiguration.createCompatibleImage( int, int, int)

Thanks!

Well, I’m by no means an expert on the subject and I only know what I’ve read, but according to The VolatleImage API User Guide located at ftp.java.sun.com/docs/j2se1.4/VolatileImage.pdf, “…offscreen images were moved to system memory in the form of the new image type: BufferedImage.” It goes on to say that “the performance of BufferedImage software rendering has improved greatly…however, there is still a need for even better performance”. At which point it simply indicates that, “a VolatileImage allows an application to take advantage of hardware acceleration”.

I also remember reading, though I can’t remember where, that if you simply made the correct calls to the super class, Image, that the jdk would determine the best derivative to use, i.e. BufferedImage or VolatileImage for the current situation. I’ve never really tried verify that method, however.

I hope that helps in answering your question. I would recommend reading the rest of that API, it’s pretty informative not just in the VolatileImage API but in the history and evolution of Swing as well to some degree.

I had read through the entire API before I really even got started playing around with this business, but before I got too far with that I started reading the forums here, which is where I heard that BufferedImages were accelerated as well…

Since that API was put out a while ago, and 1.4.2 is now out and all, I thought maybe someone could give me the straight answer on the performance of BufferedImages vs. VolatileImages… Perhaps I’ll have to do the testing myself…

I appreciate all your input though!

Search the forums, its been covered many times before.

but cos i’m nice, i’ll summarise it here :slight_smile:

Images returned from these methods are ‘ManagedImages’ (previously known as Automatic Images)
This means they are elligable for caching in VRAM, and hence are hardware accelerated.
:-

Toolkit.createImage()
Toolkit.getImage()
GraphicsConfiguration.createCompatibleImage()
Component.createImage()

These slides maybe helpful :-
http://servlet.java.sun.com/javaone/resources/content/sf2003/conf/sessions/pdfs/1402.pdf

and also, this example program :-
http://www.pkl.net/~rsc/downloads/Balls.jar

Images returned from ImageIO.read() are not accelerated (in the current java version, though I believe the intention is to make them so)
If you want to load images using ImageIO, you must then subsequently copy the image into a ManagedImage.
The code to do that will look something like this :-

import java.awt.*;
import java.awt.image.*;
public class ImageLoader
{
   final GraphicsConfiguration gc;
   public ImageLoader(GraphicsConfiguration gc)
   {
      if(gc==null)
      {
         gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
      }
      this.gc = gc;
   }

   public BufferedImage loadImage(String resource, int transparencyType)
   {
      try
      {
         BufferedImage src = javax.imageio.ImageIO.read(getClass().getResource(resource));
         //Images returned from ImageIO are NOT managedImages
         //Therefor, we copy it into a ManagedImage
         BufferedImage dst = gc.createCompatibleImage(src.getWidth(),src.getHeight(),transparencyType);
         Graphics2D g2d = dst.createGraphics();
         g2d.setComposite(AlphaComposite.Src);
         g2d.drawImage(src,0,0,null);
         g2d.dispose();
         return dst;
      }
      catch(java.io.IOException e)
      {
         return null;
      }
   }
}

Currently accelerated Transparency is only possible using ManagedImages. (not with VolatileImages)

Also, currently only bitmask transparency is by default accelerated.
However, there are flags to enabled hardware acceleration for images with a full alpha channel (Translucent). These flags also make AlphaComposite operations hardware accelerated.

The code to set the flags is :-

System.setProperty("sun.java2d.translaccel", "true");
System.setProperty("sun.java2d.ddforcevram", "true");

This Thread covers the flags in more detail
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1048663269;start=0#0

Abuse,

Many thanks for that. I’ve been reading through the forums and APIs and trying to sort out the truth from the “other stuff”. I really appreciate you putting together that fine list of links, as well as all the other info. Hopefully armed with this new data I can eliminate most of the graphics headaches I’ve been having of late!

–Zaphod

I guess I’m still too much of a newb to be of much use to anyone. Oh well, I’m glad you asked to question though, I learned much more than I expected I would today from Abuse’s great summary and explanation. ;D