Starling-Framework icon indicating copy to clipboard operation
Starling-Framework copied to clipboard

Reuse images

Open Deirel opened this issue 7 years ago • 2 comments

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?

Deirel avatar Apr 06 '17 08:04 Deirel

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?

PrimaryFeather avatar Apr 07 '17 09:04 PrimaryFeather

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.

johncridges avatar May 17 '17 23:05 johncridges