Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I have a problem with VolatileImages?  (Read 4922 times)
0 Members and 1 Guest are viewing this topic.
Offline cypher_516

Junior Newbie





« Posted 2010-05-11 21:42:02 »


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
package MyGame;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;

/**
 * Extended volatile image class
 *
 * @author 10uR
 */

public class VImage
{

    private BufferedImage sourceImage;
    private VolatileImage volatileImage;
    private static GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private static GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();

    public VImage(BufferedImage sourceImage)
    {
        this.sourceImage = sourceImage;
    }

    public Image getVolatileImage()
    {
        if(Program.useVolatileImages){
            if (volatileImage == null || volatileImage.validate(gc) != VolatileImage.IMAGE_OK)
                reload();

            return volatileImage;
        }
        return sourceImage;
    }

    public void setSourceImage(BufferedImage sourceImage)
    {
        this.sourceImage = sourceImage;
    }

    private void reload()
    {
        do
            volatileImage = getVolatileImageFromImage(sourceImage);
        while (volatileImage.contentsLost());
    }

    public static VolatileImage getVolatileImageFromImage(BufferedImage image)
    {
        VolatileImage volatileImage = gc.createCompatibleVolatileImage(image.getWidth(null), image.getHeight(null), image.getTransparency());
        Graphics2D g = (Graphics2D) volatileImage.getGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setBackground(new Color(0, 0, 0, 0));
        g.clearRect(0, 0, volatileImage.getWidth(), volatileImage.getHeight());
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return volatileImage;
    }

}


This is what i wrote for handle volatile images like explained here: http://gpwiki.org/index.php/Java:Tutorials:VolatileImage
To test this VImage class i made a program that has 2 balls which can controllable form keyboard and added a 1920x1200 background image.It worked well with volatile images on window mode but when i toggle full screen mode fps dropped down immediately from 60 to 40 and sometimes i got heap space exception.After set useVolatileImages as false fps increased again and working perfect.Now i'm wondering am i doing something wrong?

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #1 - Posted 2010-12-23 09:13:42 »


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
package MyGame;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;

/**
 * Extended volatile image class
 *
 * @author 10uR
 */

public class VImage
{

    private BufferedImage sourceImage;
    private VolatileImage volatileImage;
    private static GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private static GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();

    public VImage(BufferedImage sourceImage)
    {
        this.sourceImage = sourceImage;
    }

    public Image getVolatileImage()
    {
        if(Program.useVolatileImages){
            if (volatileImage == null || volatileImage.validate(gc) != VolatileImage.IMAGE_OK)
                reload();

            return volatileImage;
        }
        return sourceImage;
    }

    public void setSourceImage(BufferedImage sourceImage)
    {
        this.sourceImage = sourceImage;
    }

    private void reload()
    {
        do
            volatileImage = getVolatileImageFromImage(sourceImage);
        while (volatileImage.contentsLost());
    }

    public static VolatileImage getVolatileImageFromImage(BufferedImage image)
    {
        VolatileImage volatileImage = gc.createCompatibleVolatileImage(image.getWidth(null), image.getHeight(null), image.getTransparency());
        Graphics2D g = (Graphics2D) volatileImage.getGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setBackground(new Color(0, 0, 0, 0));
        g.clearRect(0, 0, volatileImage.getWidth(), volatileImage.getHeight());
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return volatileImage;
    }

}


This is what i wrote for handle volatile images like explained here: http://gpwiki.org/index.php/Java:Tutorials:VolatileImage
To test this VImage class i made a program that has 2 balls which can controllable form keyboard and added a 1920x1200 background image.It worked well with volatile images on window mode but when i toggle full screen mode fps dropped down immediately from 60 to 40 and sometimes i got heap space exception.After set useVolatileImages as false fps increased again and working perfect.Now i'm wondering am i doing something wrong?



I don't know who wrote that Tutorial, but it's potencially broken.

The Copying from a VolatileImage example does not account for the potencial of the VolatileImage becoming invalidated in the time-frame between the image being recreated & the image being drawn onto it's destination.

1  
2  
3  
4  
5  
6  
7  
   if (vimage.validate(gc) != VolatileImage.IMAGE_OK) {
      vimage = createVolatileImage(vimage.getWidth(), vimage.getHeight(), vimage.getTransparency());
      render(); // This is coming up in Code Example 4.
  }

// what happens if vimage becomes invalid here?
  g.drawImage(vimage,x,y,null);



To work around this limitation the drawImage itself has to be part of a loop; with the validity check *after* the drawImage. This ensures the VolatileImage is drawn atleast once while in a valid state.

However this exposes a more fundamental flaw in the entire VolatileImage API when dealing with translucent VolatileImages or when using VolatileImages in conjunction with more exotic Compositing rules.

The above suggested draw loop ensures the VolatileImage is drawn to the destination successfully atleast once; and this is fine if you are simply replacing the pixel components of the destination.
However if you are performing a composition (either an AlphaComposite or some kind of custom colour composite) you will run into trouble because there is no way to guarantee that the VolatileImage was drawn just once.

Basically VolatileImages cannot (in all cases) be drawn onto another surface deterministically. A bit of a serious & fundamental API flaw TBH.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Online CommanderKeith
« Reply #2 - Posted 2010-12-23 09:35:25 »

I've played with VolatileImages a lot lately and find that they don't work in Applets, but do in JFrames.

Also, they are not meant to be necessary since a BufferedImage is meant to use a backing VolatileImage for hardware-acceleration and take care of the 'volatility' of the surface for you automatically, but in my tests VolatileImages run faster than BufferedImages by about 30 - 40%.
Try running this demo and press 'I' to toggle between BufferedImages and VolatileImages and see the change in FPS.
http://www.keithwoodward.com/straightedge/straightedge.jnlp

Try running this applet and pressing 'I' once and watch it do nothing. VI's don't seem to work in applets Sad
http://www.keithwoodward.com/straightedge

@ cypher_516 if you want to see the code where I use VolatileImages, download the source for my project and check out  straightedge.test.demo.AcceleratedImage and straightedge.test.demo.ViewPane.
https://code.google.com/p/straightedge/downloads/list

Keith

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #3 - Posted 2010-12-23 09:56:33 »

Quote
Try running this demo and press 'I' to toggle between BufferedImages and VolatileImages and see the change in FPS.
I see no noticeable change in fps in either example.

Current project - Rename and Sort
Online CommanderKeith
« Reply #4 - Posted 2010-12-23 10:05:58 »

Interesting, thanks for letting me know. There must be something funny going on in the pipeline. Some computers do, some don't.

I remember that Dmitri Trembovetski, who works on Java2D at oracle/sun, said that BufferedImages were not meant to be inferior to VolatileImages because under the hood BI's are backed by VI's, but on my computer and others it's not the case.

PS: make sure you clicked inside the applet and/or jframe when you press I or it won't do anything.

cheers,
Keith

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #5 - Posted 2010-12-23 12:20:39 »

I've played with VolatileImages a lot lately and find that they don't work in Applets, but do in JFrames.

Also, they are not meant to be necessary since a BufferedImage is meant to use a backing VolatileImage for hardware-acceleration and take care of the 'volatility' of the surface for you automatically, but in my tests VolatileImages run faster than BufferedImages by about 30 - 40%.
Try running this demo and press 'I' to toggle between BufferedImages and VolatileImages and see the change in FPS.
http://www.keithwoodward.com/straightedge/straightedge.jnlp

Try running this applet and pressing 'I' once and watch it do nothing. VI's don't seem to work in applets Sad
http://www.keithwoodward.com/straightedge

@ cypher_516 if you want to see the code where I use VolatileImages, download the source for my project and check out  straightedge.test.demo.AcceleratedImage and straightedge.test.demo.ViewPane.
https://code.google.com/p/straightedge/downloads/list

Keith

In the first one, my fps was steady at about 130 fps. When I pressed I the fps went down to about 80.That's a huge difference.
The applet one refused to render anything when I pressed I
Offline zammbi

JGO Coder


Medals: 4



« Reply #6 - Posted 2010-12-23 22:30:20 »

Quote
PS: make sure you clicked inside the applet and/or jframe when you press I or it won't do anything.
Yeah I did. It might be because I have a Intel graphics card which I believe Java still doesn't turn hardware acceleration on.

Current project - Rename and Sort
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #7 - Posted 2011-05-11 13:42:34 »

0.o thanks zammbi. hadnt realised that was why my laptop can barely run my game
Offline JL235

JGO Coder


Medals: 10



« Reply #8 - Posted 2011-05-19 08:58:25 »

The various hardware acceleration and video ram optimizations behind the scenes in Java2D have always been flakey at best. Recently where I worked we experimented with turning on the OpenGL pipeline by default for something we work on, and it became unstable on certain OS's.

IMHO if you really want hardware acceleration then use Slick, JOGL, LWJGL, WebGL powered by GWT or an applet, or something else.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (35 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (32 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (35 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!