Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: For how long does OpenGL store a texture?

How long does openGL store a texture for?

Does the texture memory get recycled when you leave an activity?

For example if I have the following code:

mGL.glGenTextures(1, mTextures, 0);
        mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]); // A bound texture is
                                                            // an active texture

        //mGL.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap.getWidth(),bitmap.getHeight(), 0, GL10.GL_RGBA, GL10.GL_FLOAT, textures);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,GL10.GL_RGBA, bitmap, 0);

        // create nearest filtered texture
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_LINEAR); // This is where the scaling algorithms are
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR); // This is where the scaling algorithms are
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_CLAMP_TO_EDGE);
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        //bitmap.recycle();
        Log.v("GLSurfaceView", "Loading Texture Finished");
        return mTextures[0];

What changes will invalidate the return value?

I don't want to reload all my textures when I come back into an activity (say the person had a phone call) because it really slows things down.

Update:

Found this info in the Renderer documentation, which confirms the answer given below by @svdree:

EGL Context Lost There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this.

That means that the textures are related to the EGL Context

Since posting this question I have attempted to solve the problem by having my activities inherit from a base activity that has a reference to a custom GLRenderer. Basically, I can pass the OpenGLSurface View forward (i.e. make it instantiate it in one activity and use it in the next), but once it goes through its shutdown procedure it doesn't start up again.

I have also found that making your activities transparent preserves the openGL context below the transparent activity (which makes sense, but helps only for menus and such). However I suppose it would be possible to always have every activity after the openGL activity be just a little bit transparent, thereby preserving the textures in the background across all your activities (this is probably what I will do)

like image 990
tjb Avatar asked Aug 15 '11 15:08

tjb


1 Answers

When you leave an activity, your OpenGL context will be lost, which means all your textures, vertex buffer objects etc. will need to be recreated. This is typically done in the onSurfaceCreated() method of the GLSurfaceView.Renderer class.

like image 158
svdree Avatar answered Sep 22 '22 21:09

svdree