Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.84k stars 819 forks source link

Reuse images #956

Open Deirel opened 7 years ago

Deirel commented 7 years ago

If there is reason to add ability to temporary unload textures from GPU memory and return it back later, to avoid disposing display objects containing Images and make possible its pooling without spending GPU memory?

PrimaryFeather commented 7 years ago

The idea is more or less to "hibernate" a texture, which means that it's going to be purged from memory until the moment a display object needs it again (at which time it would be restored from its original source, just like after a context loss). Is that correct?

johncridges commented 7 years ago

We do this in our project. We "hide" the textures (by disposing the base) to free the GPU memory and as long as you don't try to render them, you're OK. When we want to start rendering them again, we "unhide" them. We use the following kludgy code (mTexture is the texture being hidden):

var mTexture:Texture;
var mOnRestoreSaved:Function;
var mIsHiding:Boolean = false;

public function set hiding(val:Boolean):void
{
    if (mIsHiding != val) {
        if (val) {
            mOnRestoreSaved = mTexture.root.onRestore;
            mTexture.root.dispose();
        }
        else {
            mTexture.root.onRestore = mOnRestoreSaved;
            if (Starling.current.contextValid) {
                mTexture.root.onContextCreated();
            }
        } 
        mIsHiding = val;
    }
}

The kicker is that the ConcreteTexture.onContextCreated method is private and we had to make it public for this to work.