PDA

View Full Version : Simple Texture Load & Unload



xandred
01-04-2013, 08:18 PM
Okay so I am making a live wallpaper for android phone but up until now, I have never noticed this issue.

Basically I am looking to see if my texture load & unload function is efficient or at least not horrible.

Breakdown of what happens on run:

-Wallpaper initialized.
-Texture manager Loops through all textures and loads them using glBindTexture.
-Wallpaper shows up running perfectly fine. Using 21mb of ram.
-I have the background flip back and forth every few seconds, both backgrounds are already loaded.
-I change background by doing this ---> paramGL10.glBindTexture(GL10.GL_TEXTURE_2D, TextureManager.textures[i]); ( I flip/ flop the integer between 0 and 1 for instance )
-On phone lock unload textures
-On phone resume, reload textures.

My ram increasing slowly but steadily. I don't understand why, all of my openGL knowledge has been self taught trial and error so I probably am not using the most efficient way of doing this.
Increases continuously I have seen it go as high as 81mb.

Here is my code.

Texture Manager Loading Textures on initial run, or after phone was resumed.



public static void loadTextures(GL10 paramGL10) {
paramGL10.glGenTextures(MAX_TEXTURES,textures,0);
bindTextureIDs(paramGL10, "blank");
bindTextureIDs(paramGL10, "grid");
etc
etc
etc




public static void bindTextureIDs(GL10 paramGL10, String fileName) {
int ID = textureIDs.size();

textureIDs.put(fileName, ID);
try {
int resID = context.getResources().getIdentifier(fileName, "drawable", ApplicationPackageName);

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources( ), resID, bitmapOptions);
paramGL10.glBindTexture(GL_TEXTURE_2D,textures[ID]);
paramGL10.glTexParameterf(GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST );
paramGL10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
paramGL10.glTexParameterf(GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
paramGL10.glTexParameterf(GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
paramGL10.glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_ MODE,GL_MODULATE);
GLUtils.texImage2D(GL_TEXTURE_2D,0,bitmap,0);
paramGL10.glFlush();
bitmap.recycle();
} catch(Exception e) { Log.v("TEST","Texture Data Corrupt, Re-download Package."); }
}


Texture Manager unloading textures on lock.



public static boolean unload(GL10 paramGL10)
{
try {
Log.v("TEST", "TextureManager Unloading Textures");
paramGL10.glDeleteTextures(MAX_TEXTURES, textures, 0);
paramGL10.glFlush();
TextureManager.textureIDs.clear();
isLoaded = false;
} catch (Exception e) {}
return true;
}


And my rendering code



public void render(GL10 paramGL10, TextureManager paramTextureManager, MeshManager paramMeshManager) {
int i = paramTextureManager.getTextureID(paramGL10, this.texName);
Mesh localMesh = paramMeshManager.getMeshByName(paramGL10, this.meshName);

paramGL10.glPushMatrix();
paramGL10.glTranslatef(this.origin.x,this.origin.y , this.origin.z);
paramGL10.glRotatef(180,0,0,1);
paramGL10.glBindTexture(GL10.GL_TEXTURE_2D, TextureManager.textures[i]);

if (this.color != null)
paramGL10.glColor4f(this.color.x, this.color.y, this.color.z, this.color.a);
paramGL10.glScalef(this.scale.x, this.scale.y,this.scale.z);
paramGL10.glRotatef(this.angles.a, this.angles.x, this.angles.y, this.angles.z);
localMesh.renderFrame(paramGL10);
paramGL10.glPopMatrix();
}


Can anyone help me explain why this is leaking my memory, I figured if I locked my phone and unlocked it after the memory leaked a lot, the unload and reload should clear the GPU memory but it does not.

I hope someone is able to help me see the error of my ways!

Thanks

xandred
01-04-2013, 09:41 PM
Update
-----------
I have read online that I should avoid using glDeleteTextures, only call texImage2D on initialize and then use glBind to reference the texture. So I have removed the glDeleteTextures, only called texImage2D on the initialize ( in this case the preview screen )
and using glBind as normal.

This shows the livewallpaper in the preview window perfectly, as soon as I set the wallpaper, my entire window goes white and all textures seem to be lost. Now because I am not unloading and reloading textures it remains white ( as before this wouldnt happen, and if it did a simple lock phone and unlock would reload the textures )

But if I upload the application while its set as my wallpaper, it works perfect. Unless I go back to the wallpaper chooser and select the live wallpaper from the menu. Then once again the textures are all white.


Ugh what is the issue?

xandred
01-04-2013, 10:29 PM
Alright more reading and it turns out many people are having issues with the pause and resume reloading textures. I re-enabled everything back to the memory leak state and have only called

paramGL10.glGenTextures(MAX_TEXTURES,textures,0);
on initialize. Seems to solve the memory leak issue. But if my wallpaper is set and I reselect it from the live wallpaper screen. my memory footprint doubles. I am going to sleep and hopefully it will come to me overnight.