Detect context loss (libGDX on Android)

Hey all,

I have a little program that simply draws a small image to a larger image and then draws the large image to the screen. It’s for a live wallpaper on Android, and it pretty much works.

I’m working with these variables:


private Pixmap smallPixmap;

private SpriteBatch batch;

private Pixmap screenPixmap;
private Texture screenTexture;

public void create(){
		batch = new SpriteBatch();
		batch.enableBlending();

		smallPixmap = new Pixmap(Gdx.files.internal("small.png"));
		screenPixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Format.RGBA8888);
		screenTexture = new Texture(screenPixmap);
}

And in the render() method, I simple draw the small image to the large image and then redraw the large image:


public void render(float delta) {	

		Gdx.gl.glClearColor(0, 0, 0, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		screenPixmap.drawPixmap(smallPixmap, 0, 0, smallPixmap.getWidth(), smallPixmap.getHeight(), MathUtils.random(screenPixmap.getWidth()), MathUtils.random(Gdx.graphics.getHeight()), smallPixmap.getWidth(), smallPixmap.getHeight());

		//THIS IS THE LINE
		screenTexture.dispose();
		screenTexture = new Texture(screenPixmap);
		
		batch.begin();
		batch.draw(screenTexture, 0, y, screenTexture.getWidth(), screenTexture.getHeight());
		batch.end();	
}

This all seems to work. The only problem is the above commented line where I dispose of the screen texture and then recreate it. I have to do this on Android because the app (in this case a live wallpaper) can lose the openGL context, which invalidates the texture. Without that line (if I just draw the screenPixmap to the screenTexture instead), the screen either displays all white or something like this:

https://pbs.twimg.com/media/BZzBw21CcAAPN_6.png

However, it seems like overkill to create a new texture instance every single frame. My question is, is there a way to detect whether the context has been lost, so I only recreate the texture when I need to? Something like this:


if(contextWasLost){
		screenTexture.dispose();
		screenTexture = new Texture(screenPixmap);
}
else{
		screenTexture.draw(screenPixmap, 0, 0);
}

I’ve tried googling, but I haven’t been able to find anything conclusive. And it’s further complicated that it works okay on some devices (including mine), but exhibits the above errors on other phones!

Why are you drawing a pixmap to another pixmap? Why not just do this:

.... init
    myTex = new Texture("blah.png");

.... render
    batch.begin();
    batch.draw(myTex, 50, 50);
    batch.end();

If you create a Texture with a path like that it will be correctly managed on context loss. If you create a Pixmap with the width and height constructor, it will be unmanaged. You will need to re-create the pixmap on resume() (which is called on context restore).

Lots more info here:

Yeah, this is an unmanaged texture, since I’m drawing to it over and over again. I’ll try the resume() function, thanks!